pandas for Data Analysis
Loading, selecting, filtering, grouping, joining and summarising tabular data with pandas DataFrames.
Intermediate · about 14 hours · difficulty 3/5
Learn first: NumPy Essentials, Jupyter Notebooks and Reproducible Work
pandas is the workhorse of Python analysis. If you know SQL, most of pandas maps onto concepts you already have.
Essentials
- Load:
pd.read_csv,read_excel,read_parquet,read_sql. Passdtypeandparse_datesto avoid type surprises. - Inspect:
df.head(),df.info(),df.describe(),df.shape,df["col"].value_counts(). - Select: columns with
df[["a", "b"]]; rows by label with.loc, by position with.iloc. - Filter:
df[df["amount"] > 100]ordf.query("amount > 100 and country == 'IN'"). - New columns:
df.assign(margin=df.revenue - df.cost). - Group and aggregate:
df.groupby("country").agg(orders=("id", "count"), revenue=("amount", "sum")). - Join:
pd.merge(orders, customers, on="customer_id", how="left", validate="many_to_one"). - Sort and rank:
sort_values,rank,nlargest.
SQL to pandas
WHERE is boolean filtering, GROUP BY is groupby, JOIN is merge, window functions are groupby().transform(), shift, rolling and cumsum.
Method chaining
Chaining steps with parentheses keeps transformations readable and avoids piles of intermediate variables.
Pitfalls: the SettingWithCopyWarning (modify with .loc or copy explicitly), silent row explosion in merges (use validate=), and looping over rows with iterrows. Move on when you can reproduce any SQL aggregation you know in pandas.
Key concepts
- Reading files with correct dtypes
- Inspecting with info, describe, value_counts
- loc, iloc and boolean filtering
- assign and derived columns
- groupby and named aggregation
- merge with validate
- transform, shift, rolling: pandas window functions
- Method chaining
- Polars as a faster alternative
Mini project
Using the Olist e-commerce CSVs, build a pandas notebook computing monthly revenue, average delivery time by state and the top 10 product categories by revenue growth.
Check yourself
How do you compute each customer's share of their country's total revenue in pandas without collapsing rows?
Free resources
- pandas user guide (pandas)
- Kaggle pandas course (Kaggle)
- Python Data Science Handbook (Jake VanderPlas)