Pandas Cheat Sheet: 50 Essential Operations
A practical Pandas cheat sheet covering 50 essential DataFrame operations including selection, filtering, grouping, merging, and data cleaning with copy-paste examples.
What you'll learn
- ✓50 essential Pandas operations you will use daily
- ✓How to select, filter, group, merge, and reshape DataFrames
- ✓Data cleaning techniques for real-world messy data
- ✓Performance tips for working with large datasets
Prerequisites
- •Basic Python knowledge
- •Pandas installed (pip install pandas)
This is a practical reference for the Pandas operations you will use most often. Each example uses realistic data and can be copied directly into your code. Bookmark this page and come back when you need a quick reminder.
Setup
import pandas as pd
import numpy as np
# Sample data used throughout
df = pd.DataFrame({
"name": ["Alice", "Bob", "Charlie", "Diana", "Eve"],
"age": [28, 35, 42, 31, 27],
"city": ["NYC", "LA", "NYC", "Chicago", "LA"],
"salary": [75000, 82000, 95000, 68000, 71000],
"department": ["Engineering", "Marketing", "Engineering", "Sales", "Marketing"],
"start_date": pd.to_datetime(["2022-01-15", "2021-06-01", "2019-03-20", "2023-02-10", "2022-09-01"]),
})
Creating DataFrames
1. From a dictionary
df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
2. From a list of dictionaries
records = [{"name": "Alice", "age": 28}, {"name": "Bob", "age": 35}]
df = pd.DataFrame(records)
3. From a CSV file
df = pd.read_csv("data.csv")
4. From a dictionary with a custom index
df = pd.DataFrame({"val": [10, 20, 30]}, index=["a", "b", "c"])
Inspecting Data
5. First and last rows
df.head(3) # First 3 rows
df.tail(3) # Last 3 rows
6. Shape, columns, and data types
df.shape # (5, 6) - rows, columns
df.columns # Index(['name', 'age', ...])
df.dtypes # Data type of each column
df.info() # Complete summary
7. Quick statistics
df.describe() # Stats for numeric columns
df.describe(include="object") # Stats for string columns
df["salary"].value_counts() # Count of each unique value
df["city"].nunique() # Number of unique values: 3
Selecting Data
8. Select a single column
df["name"] # Returns a Series
df[["name"]] # Returns a DataFrame
9. Select multiple columns
df[["name", "salary", "department"]]
10. Select rows by position
df.iloc[0] # First row as Series
df.iloc[0:3] # First 3 rows
df.iloc[[0, 2, 4]] # Rows at positions 0, 2, 4
11. Select rows by label
df.loc[0] # Row with index label 0
df.loc[0:2, "name":"city"] # Rows 0-2, columns name through city
12. Select specific cells
df.at[0, "name"] # Single value (fast)
df.iat[0, 0] # Single value by position (fast)
Filtering Rows
13. Simple condition
df[df["age"] > 30]
14. Multiple conditions
df[(df["age"] > 30) & (df["city"] == "NYC")]
df[(df["department"] == "Engineering") | (df["department"] == "Marketing")]
15. Filter with isin
df[df["city"].isin(["NYC", "LA"])]
16. Filter with string methods
df[df["name"].str.startswith("A")]
df[df["name"].str.contains("li", case=False)]
17. Filter with query (cleaner syntax)
df.query("age > 30 and city == 'NYC'")
df.query("salary > @min_salary", local_dict={"min_salary": 70000})
18. Filter nulls
df[df["salary"].notna()] # Keep non-null
df[df["salary"].isna()] # Keep only null
Adding and Modifying Columns
19. Add a new column
df["bonus"] = df["salary"] * 0.1
20. Conditional column with np.where
df["seniority"] = np.where(df["age"] > 35, "Senior", "Junior")
21. Conditional column with multiple conditions
conditions = [
df["salary"] >= 90000,
df["salary"] >= 75000,
df["salary"] < 75000,
]
choices = ["High", "Medium", "Low"]
df["salary_band"] = np.select(conditions, choices)
22. Apply a function
df["name_upper"] = df["name"].apply(str.upper)
df["tax"] = df["salary"].apply(lambda s: s * 0.3 if s > 80000 else s * 0.2)
23. Rename columns
df.rename(columns={"name": "full_name", "city": "location"}, inplace=True)
24. Drop columns
df.drop(columns=["bonus", "seniority"], inplace=True)
Sorting
25. Sort by one column
df.sort_values("salary", ascending=False)
26. Sort by multiple columns
df.sort_values(["department", "salary"], ascending=[True, False])
27. Sort the index
df.sort_index()
Grouping and Aggregation
28. Basic groupby
df.groupby("department")["salary"].mean()
29. Multiple aggregations
df.groupby("department")["salary"].agg(["mean", "min", "max", "count"])
30. Named aggregations
df.groupby("department").agg(
avg_salary=("salary", "mean"),
headcount=("name", "count"),
oldest=("age", "max"),
)
31. Group by multiple columns
df.groupby(["department", "city"])["salary"].mean()
32. Transform (broadcast result back to original shape)
df["dept_avg_salary"] = df.groupby("department")["salary"].transform("mean")
df["salary_vs_avg"] = df["salary"] - df["dept_avg_salary"]
Merging and Joining
33. Inner merge
departments = pd.DataFrame({
"department": ["Engineering", "Marketing", "Sales"],
"budget": [500000, 200000, 300000],
})
merged = df.merge(departments, on="department", how="inner")
34. Left merge (keep all rows from left)
merged = df.merge(departments, on="department", how="left")
35. Merge on different column names
merged = df.merge(other_df, left_on="dept_id", right_on="id")
36. Concatenate DataFrames
combined = pd.concat([df1, df2], ignore_index=True) # Stack vertically
combined = pd.concat([df1, df2], axis=1) # Side by side
Handling Missing Data
37. Check for missing values
df.isna().sum() # Count nulls per column
df.isna().sum().sum() # Total nulls in entire DataFrame
38. Fill missing values
df["salary"].fillna(0) # Fill with constant
df["salary"].fillna(df["salary"].median()) # Fill with median
df.fillna(method="ffill") # Forward fill
39. Drop rows with missing values
df.dropna() # Drop rows with any null
df.dropna(subset=["salary"]) # Drop only if salary is null
df.dropna(thresh=4) # Keep rows with at least 4 non-null values
40. Replace values
df["city"].replace({"NYC": "New York", "LA": "Los Angeles"})
Reshaping
41. Pivot table
pivot = df.pivot_table(
values="salary",
index="department",
columns="city",
aggfunc="mean",
fill_value=0,
)
42. Melt (unpivot)
wide_df = pd.DataFrame({
"name": ["Alice", "Bob"],
"math": [90, 85],
"science": [88, 92],
})
long_df = wide_df.melt(id_vars="name", var_name="subject", value_name="score")
43. Explode a list column
df = pd.DataFrame({"name": ["Alice", "Bob"], "skills": [["Python", "SQL"], ["Java"]]})
df.explode("skills")
Date Operations
44. Extract date components
df["year"] = df["start_date"].dt.year
df["month"] = df["start_date"].dt.month
df["day_of_week"] = df["start_date"].dt.day_name()
45. Filter by date range
df[df["start_date"].between("2022-01-01", "2022-12-31")]
46. Calculate date differences
df["tenure_days"] = (pd.Timestamp.now() - df["start_date"]).dt.days
String Operations
47. Common string methods
df["name"].str.lower()
df["name"].str.upper()
df["name"].str.strip()
df["name"].str.replace("Alice", "Alicia")
df["name"].str.split(" ")
df["name"].str.len()
Export
48. Save to CSV
df.to_csv("output.csv", index=False)
49. Save to Excel
df.to_excel("output.xlsx", index=False, sheet_name="Data")
50. Save to JSON
df.to_json("output.json", orient="records", indent=2)
Performance Tips
When working with large DataFrames (millions of rows), these patterns make a noticeable difference.
# Use vectorized operations instead of apply
# Slow:
df["bonus"] = df["salary"].apply(lambda x: x * 0.1)
# Fast:
df["bonus"] = df["salary"] * 0.1
# Use category dtype for low-cardinality strings
df["department"] = df["department"].astype("category")
# Reduces memory by 90%+ for columns with few unique values
# Read only needed columns from CSV
df = pd.read_csv("big_file.csv", usecols=["name", "salary", "department"])
# Use chunked reading for files that don't fit in memory
chunks = pd.read_csv("huge_file.csv", chunksize=100000)
for chunk in chunks:
process(chunk)
Wrapping Up
These 50 operations cover the vast majority of what you will do with Pandas day to day. The key patterns to internalize are: vectorized operations over apply, boolean indexing for filtering, groupby with agg for aggregation, and merge for combining DataFrames. Keep this page bookmarked as a quick reference when you need a syntax reminder.
Related articles
- Pandas Pandas read_csv: Every Option Explained with Examples
Master pandas read_csv with practical examples for every important parameter including dtypes, parsing dates, handling missing data, chunked reading, and encoding.
- Pandas Pandas GroupBy and Aggregation Tutorial
Master pandas groupby with single and multi-column aggregations, named outputs, transform, and filter for clean analytical pipelines.
- Pandas Pandas Plotting: Built-in Visualization Guide
Create effective charts directly from Pandas DataFrames with plot(), including line, bar, histogram, scatter, box plots, and customization tips.
- Pandas Pandas: apply vs Vectorization
When to reach for .apply and when vectorized operations win. A practical comparison with benchmarks, mental models, and the patterns that keep Pandas code both readable and fast.