Pandas Cheat Sheet
Pandas data manipulation, filtering, grouping, and analysis techniques.
2 PagesIntermediateMay 10, 2026
Reading Data
Load data into a DataFrame.
python
import pandas as pddf = pd.read_csv("data.csv")df.head() # First 5 rowsdf.info() # Column types & nullsdf.describe() # Summary statistics
Selecting Data
Access rows and columns.
python
df["age"] # Single column (Series)df[["name", "age"]] # Multiple columnsdf.loc[0, "name"] # By labeldf.iloc[0, 1] # By positiondf[df["age"] > 18] # Boolean filter
Grouping & Aggregation
Summarize data by group.
python
df.groupby("category")["price"].mean()df.groupby("category").agg({"price": "sum", "id": "count"})df.sort_values("price", ascending=False)
Cleaning Data
Handle missing or duplicate values.
python
df.dropna() # Drop rows with NaNdf.fillna(0) # Fill NaN with a valuedf.drop_duplicates() # Remove duplicate rowsdf["price"] = df["price"].astype(float)
Merging & Joining
Combine DataFrames with merge, join, and concat.
python
# SQL-style joins on a keypd.merge(left, right, on="id", how="inner") # inner/left/right/outerpd.merge(left, right, left_on="a", right_on="b")# Join on indexleft.join(right, how="left", lsuffix="_l", rsuffix="_r")# Stack rows or columnspd.concat([df1, df2], axis=0, ignore_index=True) # rowspd.concat([df1, df2], axis=1) # columns
Pivot & Reshape
Reshape between wide and long formats.
python
# Long -> widedf.pivot_table(index="date", columns="city", values="temp", aggfunc="mean")# Wide -> longdf.melt(id_vars=["date"], var_name="city", value_name="temp")# Multi-index stackingdf.set_index(["date", "city"]).stack()df.unstack(level=-1)# Cross-tabulationpd.crosstab(df["city"], df["weather"])
Time Series
Datetime indexing, resampling, and rolling windows.
python
df["ts"] = pd.to_datetime(df["ts"])df = df.set_index("ts")# Resample to daily meansdf.resample("D").mean()# Rolling & expanding windowsdf["roll"] = df["value"].rolling(window=7).mean()df["cum"] = df["value"].expanding().sum()# Shift / percent changedf["pct"] = df["value"].pct_change()df["lag1"] = df["value"].shift(1)
Apply, Map & Transform
When to use each row/column transformation method.
- df.apply(fn, axis=1)- run a function across each row (axis=1) or column (axis=0)
- Series.map(fn)- element-wise mapping on a single column, also accepts a dict lookup
- df.applymap(fn)- element-wise on every cell (deprecated for df.map in pandas 2.1+)
- groupby.transform(fn)- returns a like-indexed result for broadcasting group stats back to rows
- df.pipe(fn)- chain a custom function into a method pipeline for readability
- df.assign(col=...)- add/overwrite columns and return a new DataFrame without mutating
Pro Tip
Chain operations with method chaining (df.dropna().sort_values(...)) for cleaner, more readable pipelines.
Was this cheat sheet helpful?
Explore Topics
#Pandas#PandasCheatSheet#DataScience#Intermediate#ReadingData#SelectingData#GroupingAggregation#CleaningData#MachineLearning#CheatSheet#SkillVeris