Reference

Pandas syntax sheet

Each piece of syntax next to what it returns. Grows with every lesson.

Select columns · L01

SyntaxReturns
df["col"]Series (one column)
df[["a", "b"]]DataFrame (list of names → table)

Masks & filtering · L01

SyntaxReturns
df["col"] > 100Boolean Series (mask). Comparing with NaN gives False.
> >= < <= == !=Comparison operators
df[mask]DataFrame of the True rows, original labels kept
df[mask][["a"]]Filter, then select columns

Combine conditions · L01

SyntaxMeans
(a) & (b)AND
(a) | (b)OR
~(a)NOT

Always put parentheses around each condition. Use the symbols, not the words and/or/not.

Aggregate · L01

SyntaxReturns
.sum() .mean() .min() .max()One number, skipping NaN
.count()The number of non-missing values
len(df)The number of rows, missing values included
.fillna(v)A new Series with the gaps filled

Sort & locate · L02

SyntaxReturns
df.sort_values("col")DataFrame sorted ascending, NaN last
…, ascending=FalseLargest first
.head(n)The first n rows
.iloc[0] / .iloc[-1]The first or last row by position (a Series)
row["col"]One value
df.loc[label, "col"]One value, looked up by row label
s.idxmax() / s.idxmin()The label where the max or min is

Group · L02

SyntaxReturns
df.groupby("key")["col"].sum()A Series indexed by key, sorted by key
….mean() .count() .max()Any aggregation, applied per group
….sum().idxmax()The key with the largest total

Summarise · L03

SyntaxReturns
s.value_counts()A Series of counts indexed by value, most common first, NaN left out
…(normalize=True) / …(dropna=False)Shares instead of counts / include NaN
….groupby("k")["c"].agg(["mean", "count"])A DataFrame: one row per key, one column per stat
df["new"] = df["c"] <= 2Creates a True/False column
df["new"].mean()The share of True (True = 1, False = 0)
s.sort_values(ascending=False)A Series sorted by its values (no column name)

Dates · L04

SyntaxReturns
df["d"] = pd.to_datetime(df["d"])Text column → real dates (do this first)
df["d"].dt.month / .dt.year / .dt.day_name()A part of each date
df[df["d"] >= "2026-03-01"]Rows on or after a date
df["d"].dt.to_period("M")Each date as its month (2026-03)
df.groupby(df["d"].dt.to_period("M"))["c"].mean()A Series per month, in time order: the trend
s.sort_index()A Series sorted by its labels (e.g. months after value_counts)
s.rolling(3).mean()Rolling average over 3 values; the first 2 are NaN

Charts · L05

SyntaxDraws
import matplotlib.pyplot as pltmatplotlib, the drawing library pandas uses
s.plot()Line chart: index along the bottom, values up the side (use for time)
s.plot.bar() / s.plot(kind="bar")Upright bars, one per label, in the Series' order (use for groups)
s.plot.barh()Horizontal bars; the first label is at the bottom
s.sort_values(ascending=False).plot.bar()Ranked bars: sort first, the chart keeps the order
.plot(title="…", xlabel="…", ylabel="…", ylim=(0, 5))Title, axis names, fixed y-axis range
plt.title("…") / plt.ylabel("…") / plt.ylim(0, 5)The same, on a line after the chart
a.plot(label="a") + b.plot(label="b") + plt.legend()Two lines on one chart, with a key
ax = a.plot() then b.plot(ax=ax)Explicitly draw onto the same chart
plt.savefig("chart.png", bbox_inches="tight")Saves the chart as an image file

Load a CSV, from disk or S3 · L06

SyntaxReturns
pd.read_csv("file.csv")DataFrame: header line → column names, empty values → NaN
pd.read_csv(…, nrows=5000)Only the first 5,000 rows
pd.read_csv(…, usecols=["a", "b"])Only those columns, in the file's order
df.shape(rows, columns): a property, no brackets
df.head(3)The first 3 rows (5 by default)
df.to_csv("copy.csv", index=False)Writes a CSV; index=False leaves out the row labels
From S3 with boto3Returns
s3 = boto3.Session(profile_name="NAME").client("s3")A client logged in with that AWS profile
r = s3.list_objects_v2(Bucket="b", Prefix="exports/")Dict: r["KeyCount"], and r["Contents"] (one dict per object: Key, Size). No "Contents" when nothing matches.
pd.DataFrame(r["Contents"])The listing as a table
obj = s3.get_object(Bucket="b", Key="exports/x.csv")Dict; obj["Body"] is the contents as a stream (read once)
pd.read_csv(obj["Body"], nrows=5000)DataFrame from the object
s3.download_file("b", "exports/x.csv", "x.csv")Saves the object to a local file
pd.read_csv("s3://b/key.csv", storage_options={"profile": "NAME"})You'll also see this: the same in one line, via the extra s3fs package
In a notebookDoes
Shift+EnterRuns the cell; variables stay until you restart
%pip install boto3Installs into the kernel's own Python
!aws sts get-caller-identity --profile NAME! runs a terminal command; this one says who that profile logs you in as

Check and clean a CSV · L07

SyntaxReturns
pd.read_csv("file.csv", na_values=["No topic"])That text comes in as NaN too
df.dtypes / df["a"].dtypeThe type pandas guessed, per column / for one column
df.info()Prints each column's dtype and non-missing count
df.isna().sum() / df.isna().mean()Series: missing count / missing share, per column
s.value_counts(dropna=False)Counts per value, with a row for NaN
df.dropna(subset=["a"])DataFrame without the rows where a is missing (no subset: any column)
(df["topic"] == "No topic").sum()Count a "nothing" written as text: isna() can't see it
s.astype(int)Whole numbers; fails if s has NaN
s.str.lower()Lower-case text, so spellings group together
df.drop(columns=["a"])DataFrame without column a (without columns= it looks for a row)
In the filedtype
text, and datesstr
5.0, and a column empty on every rowfloat64
True/False with gapsobject (no gaps: bool)

Each fix returns something new: assign it back (df["a"] = …, df = df.drop(…)) to keep it.

Reading the API docs · L05

Part of a pageWhat it tells you
API reference (pandas 3.0)Every method, one page each; search for the method you're calling
ParametersEvery keyword argument: its name, the type it accepts, and its default
**kwargsExtra arguments passed on to another function: follow its link for the rest
ReturnsWhat you get back (Series, DataFrame, Axes…)
See also / ExamplesRelated methods, and short runnable examples

Checked against pandas 3.0.2, the version the sandbox runs · Python for Data Analysis, 3e · Pandas gotchas