Pandas Cheat Sheet

Read, select, clean, group and export DataFrames.

Almost every data job I've done starts the same way. Someone sends an Excel file, and before any analysis can happen it has to be read in, checked, cleaned, grouped and sent back. This table covers that loop, in roughly the order you'll need each step.

Every line assumes the usual import: import pandas as pd. The notes below the table cover the mistakes that cost me the most time when I was learning.

Task Code
Read CSV pd.read_csv("f.csv")
Read Excel pd.read_excel("f.xlsx", sheet_name=0)
Info df.info(); df.describe()
Select df["col"], df.loc[mask, cols], df.iloc[0:5]
Filter df.query("sales > 1000")
Missing df.isna().sum(); df.fillna(0); df.dropna()
Duplicates df.drop_duplicates()
New column df["gst"] = df["amt"] * 0.18
Group df.groupby("city")["sales"].agg(["sum", "mean"])
Pivot df.pivot_table(index="a", columns="b", values="c", aggfunc="sum")
Merge a.merge(b, on="id", how="left")
Sort df.sort_values("sales", ascending=False)
Dates pd.to_datetime(df["d"], dayfirst=True).dt.month
Export df.to_excel("out.xlsx", index=False)

Notes from real files

Check before you trust. Run df.info() first, every time. If a column of amounts shows as object instead of a number type, something in it isn't a number: a comma, a "₹", a "-" or a stray word. pd.to_numeric(df["amt"], errors="coerce") converts what it can, and df[df["amt"].isna()] shows you what it couldn't.

Indian dates need dayfirst=True. Without it, 03-04-2026 becomes 4 March. Pandas won't warn you, and the error only shows up when the monthly totals look odd.

loc vs iloc. loc uses labels (column names, index values, true/false masks). iloc uses positions, like list indexing. If you're filtering rows by a condition, you want loc: df.loc[df["city"] == "Pune", ["name", "sales"]].

The SettingWithCopyWarning. If you filter a DataFrame and then change the result, pandas warns you because it isn't sure if you meant to change the original. Add .copy() when you filter (small = df[df.x > 0].copy()) and the warning goes away, along with the bugs behind it.

Leading zeros disappear. PIN codes, employee IDs and phone numbers get read as numbers, so 0712 becomes 712. Fix it at read time: pd.read_csv("f.csv", dtype={"pincode": str}).

merge can multiply rows. If the key isn't unique on the right-hand side, a left merge creates one row per match. Add validate="many_to_one" and pandas will stop you instead of silently doubling your sales.

Exporting to Excel. index=False stops pandas adding a column of 0, 1, 2… that nobody asked for. You need openpyxl installed for .xlsx files.