Skip to content
Laviyx Learn

Try

pandas and NumPy Cheat Sheet

Loading, inspecting, selecting, filtering, cleaning, grouping, pivoting, merging, dates, statistics and plotting with pandas and NumPy.

Updated

# → value shows what an expression returns. # name → value shows what a variable holds after that line.

Imports and loading#

Python
import numpy as np
import pandas as pd
from io import StringIO

csv_text = "name,city,amount\nAsha,Mumbai,250\nRavi,Delhi,120\nMeera,Pune,560\nJohn,Delhi,90"
df = pd.read_csv(StringIO(csv_text))   # pd.read_csv("file.csv") for a real file
df.shape                               # → (4, 3)
df.columns.tolist()                    # → ['name', 'city', 'amount']

Other readers: pd.read_excel("f.xlsx", sheet_name=...), pd.read_json(...), pd.read_sql(query, conn). Useful options: usecols, parse_dates, index_col, dtype, sep, chunksize.

NumPy essentials#

Python
a = np.array([3, 1, 4, 1, 5])
a.mean(), np.median(a), a.max()       # → (np.float64(2.8), np.float64(3.0), np.int64(5))
a[a > 2]                               # → array([3, 4, 5])
np.where(a > 2, "hi", "lo")            # → array(['hi', 'lo', 'hi', 'lo', 'hi'], dtype='<U2')
np.arange(6).reshape(2, 3).sum(axis=0)   # → array([3, 5, 7])

Inspecting#

Python
df.head(2)["name"].tolist()            # → ['Asha', 'Ravi']
df["city"].value_counts().to_dict()    # → {'Delhi': 2, 'Mumbai': 1, 'Pune': 1}
df["amount"].describe()["mean"]        # → np.float64(255.0)
df.dtypes["amount"]                    # → dtype('int64')

Also: df.info(), df.tail(), df.sample(5), df.isna().sum(), df.nunique().

Selecting#

Python
df["amount"].sum()                     # → np.int64(1020)
df.loc[0, "name"]                      # → 'Asha'
df.iloc[-1, 1]                         # → 'Delhi'
df.loc[df["amount"] > 200, "name"].tolist()   # → ['Asha', 'Meera']
df[["name", "amount"]].shape           # → (4, 2)

loc = labels (slice end included); iloc = positions (end excluded).

Filtering#

Python
df[(df["city"] == "Delhi") & (df["amount"] > 100)]["name"].tolist()   # → ['Ravi']
df[df["city"].isin(["Pune", "Mumbai"])]["name"].tolist()              # → ['Asha', 'Meera']
df[df["amount"].between(100, 300)]["name"].tolist()                   # → ['Asha', 'Ravi']
df.query("amount < 200")["name"].tolist()                             # → ['Ravi', 'John']

Combine conditions with &, |, ~ and parentheses.

Sorting and new columns#

Python
df.sort_values("amount", ascending=False)["name"].tolist()   # → ['Meera', 'Asha', 'Ravi', 'John']
df.nlargest(1, "amount")["name"].tolist()                     # → ['Meera']
df["gst"] = (df["amount"] * 0.18).round(1)
df["gst"].tolist()                     # → [45.0, 21.6, 100.8, 16.2]
df["size"] = np.where(df["amount"] >= 200, "big", "small")
df["size"].tolist()                    # → ['big', 'small', 'big', 'small']
df = df.rename(columns={"amount": "total"}).drop(columns=["gst"])
df.columns.tolist()                    # → ['name', 'city', 'total', 'size']

Cleaning#

Python
s = pd.Series([" Mumbai", "delhi ", None, "PUNE"])
s.str.strip().str.title().tolist()     # → ['Mumbai', 'Delhi', None, 'Pune']
s.isna().sum()                         # → np.int64(1)
s.fillna("Unknown").tolist()           # → [' Mumbai', 'delhi ', 'Unknown', 'PUNE']
pd.to_numeric(pd.Series(["1,200", "x"]).str.replace(",", ""), errors="coerce").tolist()   # → [1200.0, nan]
pd.Series([1, 1, 2]).drop_duplicates().tolist()   # → [1, 2]

Also: dropna(subset=..., thresh=...), astype("category"), pd.to_datetime(col, format=...), str.split(expand=True), str.extract(pattern).

Grouping and pivoting#

Python
sales = pd.DataFrame({"region": ["N", "S", "N", "S"], "month": ["Jan", "Jan", "Feb", "Feb"], "rev": [100, 80, 120, 90]})
sales.groupby("region")["rev"].sum().to_dict()        # → {'N': 220, 'S': 170}
sales.groupby("region").agg(total=("rev", "sum"), best=("rev", "max")).loc["N"].tolist()   # → [220, 120]
sales.groupby("region")["rev"].transform("sum").tolist()   # → [220, 170, 220, 170]
pd.pivot_table(sales, index="region", columns="month", values="rev", aggfunc="sum").loc["N", "Feb"]   # → np.int64(120)
pd.crosstab(sales["region"], sales["month"]).loc["S", "Jan"]   # → np.int64(1)

Combining#

Python
left = pd.DataFrame({"id": [1, 2, 3], "name": ["A", "B", "C"]})
right = pd.DataFrame({"id": [1, 1, 4], "amt": [5, 7, 9]})
len(pd.merge(left, right, on="id"))                # → 2
len(pd.merge(left, right, on="id", how="left"))    # → 4
len(pd.merge(left, right, on="id", how="outer"))   # → 5
len(pd.concat([left, left], ignore_index=True))    # → 6

Dates and time series#

Python
d = pd.to_datetime(pd.Series(["2026-01-05", "2026-02-11", "2026-03-30"]))
d.dt.month.tolist()                    # → [1, 2, 3]
d.dt.day_name().tolist()               # → ['Monday', 'Wednesday', 'Monday']
(d.max() - d.min()).days               # → 84
ts = pd.Series(range(1, 60), index=pd.date_range("2026-01-01", periods=59, freq="D"))
ts.resample("ME").sum().tolist()       # → [496, 1274]
ts.rolling(7).mean().iloc[6]           # → np.float64(4.0)
pd.Series([100, 120, 90]).pct_change().round(2).tolist()   # → [nan, 0.2, -0.25]

Statistics#

Python
from scipy import stats

x = pd.Series([2, 4, 4, 5, 7, 9])
round(x.std(), 2), x.quantile(0.75)    # → (2.48, np.float64(6.5))
round(x.corr(pd.Series([1, 3, 2, 5, 6, 8])), 3)   # → np.float64(0.971)
round(stats.ttest_ind([5, 6, 7, 6], [8, 9, 9, 10]).pvalue, 4)   # → np.float64(0.002)
round(stats.norm.cdf(1.96), 3)         # → np.float64(0.975)

Plotting#

Python
import matplotlib
matplotlib.use("Agg")                  # not needed in Jupyter
import matplotlib.pyplot as plt

ax = sales.groupby("region")["rev"].sum().plot.bar(title="Revenue by region")
type(ax).__name__                      # → 'Axes'
plt.close("all")

df.plot() line, .plot.bar(), .plot.hist(bins=…), .plot.box(), .plot.scatter(x=…, y=…); seaborn: sns.barplot, sns.boxplot, sns.histplot, sns.scatterplot, sns.heatmap(df.corr(), annot=True).

Want the full explanations? Go to the Learn Python for Data Analysis course →