Cheat sheets / pandas cheat sheet

pandas cheat sheet

The pandas commands you'll use every day, from loading a CSV to grouping, merging, cleaning and saving. Import it as import pandas as pd.

Load and save

df = pd.read_csv("sales.csv", parse_dates=["date"])
read CSV
pd.read_excel("file.xlsx", sheet_name="Q1")
read Excel (needs openpyxl)
pd.DataFrame({"a": [1, 2], "b": ["x", "y"]})
create from a dict
df.to_csv("out.csv", index=False)
save CSV
df.to_parquet("out.parquet")
fast, typed file format

Inspect

df.head(), df.tail(3), df.sample(5)
look at rows
df.shape, df.columns, df.dtypes
size, names, types
df.info(), df.describe()
summary and statistics
df["city"].value_counts()
count each value
df.isna().sum()
missing values per column

Select and filter

df["units"], df[["city", "units"]]
one or several columns
df[df["units"] > 10]
rows matching a condition
df[(df["a"] > 1) & (df["b"] == "x")]
combine with & and |, use brackets
df.query("units > 10 and city == 'Pune'")
the same as text
df.loc[0, "city"], df.iloc[0, 0]
by label / by position
df[df["city"].isin(["Pune", "Ghent"])]
value in a list

Change

df["revenue"] = df["units"] * df["price"]
new column
df.rename(columns={"qty": "units"})
rename
df.drop(columns=["tmp"])
remove columns
df["name"].str.strip().str.lower()
string methods on a column
df["date"].dt.month
date parts
df.sort_values("revenue", ascending=False)
sort

Group and combine

df.groupby("city")["revenue"].sum()
total per group
df.groupby("city").agg(total=("revenue", "sum"), n=("id", "count"))
several named results
df.pivot_table(index="city", columns="product", values="units", aggfunc="sum")
pivot table
orders.merge(customers, on="customer_id", how="left")
join two tables
pd.concat([df1, df2], ignore_index=True)
stack tables

Missing values

df.dropna(), df.dropna(subset=["email"])
drop rows with gaps
df.fillna({"age": df["age"].median()})
fill per column
df.drop_duplicates()
remove duplicate rows
df["price"] = pd.to_numeric(df["price"], errors="coerce")
bad text becomes NaN

PythonMates · pythonmates.com/cheat-sheets/pandas/ · Python 3.15 · updated 2026-11-19