Cheat sheet, 5 min read, updated 30 September 2026

Python and pandas cheat sheet

The pandas commands analysts use every day, on one printable page.

PythonData analysis
All cheat sheets and guides
All guides and cheat sheets

Read and write

Python
import pandas as pd
import numpy as np
df = pd.read_csv("file.csv", parse_dates=["date"])
df = pd.read_excel("file.xlsx", sheet_name="Data")
df = pd.read_parquet("file.parquet")
df.to_csv("out.csv", index=False)
df.to_excel("out.xlsx", index=False)
df.to_parquet("out.parquet")

Inspect

Python
df.head(10); df.tail()
df.shape            # (rows, columns)
df.info()           # types and nulls
df.describe()       # numeric stats
df["col"].value_counts()
df.isna().sum()     # nulls per column

Select

Python
df["col"]                    # one column (Series)
df[["a", "b"]]               # several columns
df.loc[rows, "col"]          # by label
df.iloc[0:5, 0:3]            # by position

Filter

Python
df[df["amount"] > 100]
df[(df["region"] == "North") & (df["amount"] > 100)]
df[df["region"].isin(["North", "West"])]
df[df["name"].str.contains("ltd", case=False)]
df.query("amount > 100 and region == 'North'")

New columns

Python
df["revenue"] = df["qty"] * df["price"]
df = df.assign(vat=lambda d: d["revenue"] * 0.2)
df["size"] = np.where(df["revenue"] > 500, "Large", "Small")
df["name"] = df["name"].str.strip().str.title()

Group and aggregate

Python
(df.groupby("region", as_index=False)
   .agg(revenue=("revenue", "sum"),
        orders=("order_id", "nunique"),
        avg=("revenue", "mean")))

Merge (VLOOKUP)

Python
df.merge(products, on="product_id", how="left",
         validate="many_to_one")
pd.concat([df1, df2], ignore_index=True)  # stack

Pivot and reshape

Python
pd.pivot_table(df, index="region", columns="month",
               values="revenue", aggfunc="sum",
               fill_value=0, margins=True)
df.melt(id_vars="region", var_name="month",
        value_name="revenue")          # wide to long

Dates

Python
df["date"] = pd.to_datetime(df["date"], dayfirst=True)
df["month"] = df["date"].dt.to_period("M")
df["weekday"] = df["date"].dt.day_name()
df.set_index("date").resample("W")["revenue"].sum()

Missing values and duplicates

Python
df.dropna(subset=["customer_id"])
df["discount"] = df["discount"].fillna(0)
df.drop_duplicates(subset="order_id", keep="last")
df["order_id"].duplicated().sum()

Sort, rank, window

Python
df.sort_values(["region", "revenue"], ascending=[True, False])
df["rank"] = df.groupby("region")["revenue"].rank(ascending=False)
df["running"] = df.groupby("customer")["amount"].cumsum()
df["prev"] = df.groupby("customer")["amount"].shift(1)
df["ma7"] = df["amount"].rolling(7).mean()

Method chaining

Python
report = (
    pd.read_csv("sales.csv", parse_dates=["date"])
      .query("status == 'completed'")
      .assign(month=lambda d: d["date"].dt.to_period("M"))
      .groupby("month", as_index=False)["amount"].sum()
)

Written by Alessandro Ecclesie Agazzi, freelance analytics engineer in London. Updated 30 September 2026.