/cheatsheets/pandas/pandas-cheat-sheet
pandas cheat sheet: read, select, filter, group and reshape
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
- tool
- pandas
- versions
- pandas 3.x
- os
- any
The pandas operations you use every day, run on one small DataFrame with pandas 3.
python -c '
import pandas as pd
df = pd.DataFrame({
"city": ["Rome", "Milan", "Rome", "Turin", "Milan"],
"month": ["jan", "jan", "feb", "feb", "feb"],
"sales": [120, 80, 150, 60, 95],
})
print("--- filter and sort"); print(df[df["sales"] > 90].sort_values("sales", ascending=False).to_string(index=False))
print("--- group and aggregate"); print(df.groupby("city")["sales"].agg(["sum", "mean"]).to_string())
print("--- pivot"); print(df.pivot_table(index="city", columns="month", values="sales", aggfunc="sum").to_string())
print("--- new column"); print(df.assign(share=(df["sales"] / df["sales"].sum()).round(2)).head(2).to_string(index=False))'Output
--- filter and sort
city month sales
Rome feb 150
Rome jan 120
Milan feb 95
--- group and aggregate
sum mean
city
Milan 175 87.5
Rome 270 135.0
Turin 60 60.0
--- pivot
month feb jan
city
Milan 95.0 80.0
Rome 150.0 120.0
Turin 60.0 NaN
--- new column
city month sales share
Rome jan 120 0.24
Milan jan 80 0.16Read and write
| Task | Code |
|---|---|
| CSV | pd.read_csv("f.csv", dtype={"id": "str"}, parse_dates=["ts"]) |
| Excel | pd.read_excel("f.xlsx", sheet_name="Sheet1") |
| Parquet | pd.read_parquet("f.parquet") |
| SQL | pd.read_sql("SELECT * FROM t", engine) |
| Write | df.to_csv("out.csv", index=False) / df.to_parquet("out.parquet") |
Inspect
| Task | Code |
|---|---|
| First rows | df.head(10) |
| Types and nulls | df.info() |
| Statistics | df.describe() |
| Count values | df["col"].value_counts() |
| Shape | df.shape |
Select and filter
| Task | Code |
|---|---|
| Columns | df[["a", "b"]] |
| Rows by label | df.loc[df["a"] > 0, ["a", "b"]] |
| Rows by position | df.iloc[0:5, 0:2] |
| Several conditions | df[(df.a > 0) & (df.b == "x")] |
| In a list | df[df["city"].isin(["Rome", "Milan"])] |
| Text match | df[df["name"].str.contains("abc", na=False)] |
Clean and transform
| Task | Code |
|---|---|
| Missing values | df.dropna() / df.fillna({"a": 0}) |
| Rename | df.rename(columns={"old": "new"}) |
| Change type | df["a"].astype("int64") / pd.to_datetime(df["ts"]) |
| Remove duplicates | df.drop_duplicates(subset=["id"]) |
| Apply a function | df["a"].map(f) (prefer vectorised operations) |
Sources
Related pages
commands · pandaspandas merge: join DataFrames on a key (inner, left, outer)pd.merge(left, right, on='id', how='left') joins DataFrames like SQL. how picks inner, left, right, outer or cross; validate= catches duplicated keys.compare · pandaspandas loc vs iloc: label-based vs position-based selectionloc selects rows and columns by label and includes the end of a slice; iloc selects by integer position and excludes it. Cases where they return different rows.flags · pandaspandas read_csv low_memory and DtypeWarning: Columns have mixed typesread_csv parses big files in chunks; when chunks infer different types it warns DtypeWarning: Columns have mixed types. Set dtype or use low_memory=False.