Data Scientist · #034 · August 26, 2026 · 1 min read
Which pandas one-liners cover most analysis work?
Load, filter, transform, group, clean, dates: the 23 pandas one-liners that carry nearly every analysis you will ship, plus the groupby pattern worth writing properly and the copy trap everyone hits once.
Get the free PDF
One page, print-ready, free to share. No signup needed.
Every notebook you will ever write starts with the same 23 lines. Here they are on one page, with the two patterns worth writing carefully. The print-ready A4 PDF is at the bottom.
Load and look
pd.read_csv('f.csv'): in.df.head()/df.shape: peek.df.info(): dtypes + null counts.df.describe(): distribution stats in one call.
Select and filter
df[['a','b']]: columns.df.loc[rows, cols]: by label.df[df.x > 0]: boolean mask.df.query('x > 0'): the readable mask.
Transform
df.assign(y=…): new column, chainable.df['x'].astype(int): cast.df.rename(columns={…}): rename.df.sort_values('x'): sort.
Group and join
df.groupby('k').agg(…): 80% of every analysis.df.merge(d2, on='k'): the SQL join.pd.concat([a,b]): stack.
Clean
df.drop_duplicates(): dedupe.df.dropna()/df.fillna(0): nulls, both directions.df['x'].str.strip()/.str.lower(): trim and case.
Dates
pd.to_datetime(s): parse first, always.s.dt.month/s.dt.year: parts.df.resample('MS').sum(): monthly totals.
The groupby you actually write
summary = (df
.groupby("country")
.agg(orders=("id", "count"),
revenue=("amount", "sum"),
avg_basket=("amount", "mean"))
.sort_values("revenue", ascending=False))
Named aggregations: readable results, no MultiIndex surprises, chainable with parentheses.
The trap: the copy that was not one
# SettingWithCopyWarning bait:
sub = df[df.x > 0]
sub["flag"] = 1 # may edit a copy
# do this instead:
sub = df[df.x > 0].copy()
sub["flag"] = 1 # yours, for sure
Filtering returns a view or a copy, and pandas decides which. An explicit .copy() ends the ambiguity, and the warning.
Frequently asked questions
What should I run first when I open a new dataset in pandas?
Four lines: df.head() to see rows, df.shape for size, df.info() for dtypes and null counts, df.describe() for distribution stats. Two minutes with these four prevents most silent errors later, especially numeric columns that loaded as text.
What is the best way to write a groupby in pandas?
Named aggregations: df.groupby('country').agg(orders=('id','count'), revenue=('amount','sum')). Each output column is named on the spot, you avoid MultiIndex columns entirely, and the chain stays readable top to bottom.
What causes SettingWithCopyWarning and how do I fix it?
Assigning into the result of a filter, like sub = df[df.x > 0] then sub['flag'] = 1. The filter may return a view or a copy, pandas decides, so your assignment may silently edit neither. The fix is one word: sub = df[df.x > 0].copy(), which makes ownership explicit.
How do I work with dates in pandas?
Parse first with pd.to_datetime(s), before any filtering or grouping. After that, parts come from the dt accessor (s.dt.month, s.dt.year) and time-based aggregation from resample, like df.resample('MS').sum() for monthly totals on a datetime index.
Get the free PDF
One page, print-ready, free to share. No signup needed.