Tutorial

Pandas Cheatsheet: The 7 Sections Every Analyst Memorises

The Pandas cheatsheet analysts actually use — inspect, select, filter, handle nulls, sort, groupby, and merge with one-liner patterns.

Anuj SainiAug 23, 20264 min read

This is the reference you keep open while wrangling — every core Pandas verb across seven sections, each with a one-line pattern and the output you expect.

What seven sections does the cheatsheet cover?

Setup -> Inspect -> Select -> Filter -> Missing -> Sort -> Group/Agg -> Join (plus exercises). It complements the Pandas fundamentals 40Q and Advanced masterclass — fundamentals teaches, this consolidates.

Ingredients: a 7-row employee DataFrame (Name, Age with one NaN Grace, City, Salary, Department) used across every section so you compare outputs side-by-side.

How do you set up and inspect?

python
import pandas as pd
import numpy as np
data = {
    'Name': ['Alice','Bob','Charlie','David','Eva','Frank','Grace'],
    'Age': [25,30,35,40,29,30, np.nan],
    'City': ['New York','Los Angeles','Chicago','Houston','New York','Chicago','New York'],
    'Salary': [70000,80000,120000,110000,72000,55000,95000],
    'Department': ['HR','Engineering','Engineering','HR','Marketing','Support','Engineering']
}
df = pd.DataFrame(data)
print(df.head())
 
print("--- Data Info ---")
df.info()
print(df.describe())

Rendered output: df.info() flags 6 non-null ages (Grace missing); describe() shows Salary mean ~87k, confirming a small-est N sanity you can quote.

How do you select, filter, and handle nulls?

python
# Select
print(df[['Name','Salary']].head(2))
print(df.loc[0, 'City'])  # New York — label-based
 
# Filter — note & and parentheses
high = df[(df['Department']=='Engineering') & (df['Salary']>90000)]
print(high)
 
# Missing
print(df.isna().sum())
print(df.dropna(subset=['Age']).shape)  # 6 rows survive
print(df['Age'].fillna(df['Age'].median()).head())

Rendered output: high isolates Charlie and Grace (120k, 95k); dropna(subset=['Age']) removes only Grace; fillna(median) replaces her NaN with 30.0.

How do you sort, group, and merge?

python
# Sort
print(df.sort_values(['Department','Salary'], ascending=[True, False]).head(3))
 
# Groupby + agg
print(df.groupby('Department')['Salary'].agg(['mean','count','max']).round(1))
 
# Value counts one-liner
print(df['City'].value_counts(normalize=True).mul(100).round(1))
 
# Pivot — cross-tab
print(pd.pivot_table(df, values='Salary', index='Department', columns='City', aggfunc='mean', fill_value=0))
Feature / Criteria

Gotcha: Chained Indexing and SettingWithCopyWarning

df[df['Age']>30]['Salary'] = 100000 looks correct but warns and may not mutate the original — it chains two indexing ops. Rewrite as df.loc[df['Age']>30, 'Salary'] = 100000 to guarantee assignment to the source.

Where to practise repetition?

Use the same df in Pandas master workbook 50Q for groupby reps, then EDA playbook when df becomes your own CSV.


Download the Notebook and Practise

This article is a walkthrough of a runnable Jupyter notebook. Download the original .ipynb and run it locally or on Colab — every code block above appears in order.

Download the Pandas Cheatsheet Notebook

Get the complete .ipynb with outputs — runs on any Python 3.10+ environment with pandas, numpy, and the libraries listed in setup.

Download .ipynb

Continue your track: Data Analyst Roadmap · Python and Pandas Guide · SQL NULL Handbook · SQL JOIN Fan-Out · Topfolio Practice · Data Analyst vs Engineer

Dataset generators where applicable are in courses/workbooks/generators/ — see citations atop for the exact *.py source for this notebook.


Frequently Asked Questions

How do you handle missing data in Pandas?

df.isna().sum() to audit, df.dropna() to remove, df['col'].fillna(value) or df['col'].fillna(df['col'].median()) to impute. Never fillna before groupby without noting the imputation.

What is the fastest way to filter in Pandas?

Boolean indexing df[df['Salary']>90000] or df.query('Salary>90000') for readability. Wrap multi-conditions in parentheses with & and |.

Why does chained indexing warn?

df[df['Age']>30]['Salary']=100 triggers SettingWithCopyWarning — the assignment may hit a copy. Rewrite as df.loc[df['Age']>30, 'Salary']=100.

When do you use pivot_table?

When you need a cross-tab: pd.pivot_table(df, values='Salary', index='Department', columns='City', aggfunc='mean', fill_value=0).

Frequently Asked Questions

How do you handle missing data in Pandas?

df.isna().sum() to audit, df.dropna() to remove, df['col'].fillna(value) or df['col'].fillna(df['col'].median()) to impute. Never fillna before groupby without noting the imputation.

What is the fastest way to filter in Pandas?

Boolean indexing df[df['Salary']>90000] or df.query('Salary>90000') for readability. Wrap multi-conditions in parentheses with & and |.

Why does chained indexing warn?

df[df['Age']>30]['Salary']=100 triggers SettingWithCopyWarning — the assignment may hit a copy. Rewrite as df.loc[df['Age']>30, 'Salary']=100.

When do you use pivot_table?

When you need a cross-tab: pd.pivot_table(df, values='Salary', index='Department', columns='City', aggfunc='mean', fill_value=0).

Anuj Saini

Written by

Anuj SainiFounder & Lead Instructor

Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.