Python Pandas Cheatsheet

Complete DataFrame operations reference — read/write, filter, groupby, merge, and more with examples

The pandas cheatsheet covers all essential DataFrame operations for data analysis in Python. From loading CSV and Excel files to filtering, grouping, merging, and exporting, find the right pandas method with runnable code examples and expected output.

50+ operations

How to Use This Pandas Cheatsheet

This pandas cheatsheet organizes DataFrame operations into logical categories. Use the search box to filter by method name or task — type "groupby", "merge", "pivot", or "datetime" to find relevant examples instantly.

Step 1: Find Your Operation

Type any keyword in the search box to filter all 50+ operations simultaneously. Browse by section for related operations grouped by task type (I/O, selection, aggregation, etc.). Click any section header to expand or collapse it.

Step 2: Copy the Code

Click the Copy button to copy the code snippet directly to clipboard. Examples include sample data and expected output (shown in green) so you can verify your results match.

Essential Setup

All examples assume: import pandas as pd and import numpy as np at the top of your script. The sample DataFrame used in examples: df = pd.DataFrame({'name': ['Alice', 'Bob', 'Carol'], 'age': [25, 30, 35], 'dept': ['Eng', 'Mkt', 'Eng'], 'salary': [70000, 65000, 80000]})

Quick Reference: Most Used Operations

The operations you'll use most often: df.head() — preview first 5 rows, df.info() — column types and null counts, df.describe() — summary statistics, df[df['col'] > value] — filter rows, df.groupby('col').agg({'val': 'sum'}) — aggregate by group, pd.merge(df1, df2, on='key') — join two DataFrames.

Frequently Asked Questions

Is this pandas cheatsheet free?

Yes, completely free with no signup. All code examples are copyable with one click.

Is my data safe when using this tool?

Yes. This cheatsheet runs entirely in your browser. No data is sent to any server.

What is the difference between loc and iloc in pandas?

loc selects rows and columns by label (index names and column names). iloc selects by integer position (0-based index). For example, df.loc[0, 'name'] accesses the row with label 0 and column 'name'. df.iloc[0, 2] accesses the first row and third column by position.

How do I filter rows in a pandas DataFrame?

Use boolean indexing: df[df['age'] > 30] returns rows where age > 30. Chain conditions with & (and) or | (or): df[(df['age'] > 30) & (df['city'] == 'NYC')]. For multiple values, use df[df['city'].isin(['NYC', 'LA'])]. For string patterns, use df[df['name'].str.contains('Smith')].

How do I read a CSV with pandas?

Use pd.read_csv('file.csv'). Common options: sep='\t' for tab-separated, header=None if no header row, names=['a','b','c'] to specify column names, index_col=0 to use first column as index, parse_dates=['date_col'] to auto-parse dates, dtype={'col': int} to set column types.

How do I handle missing values in pandas?

Check for nulls: df.isnull().sum() counts NaN per column. Drop rows: df.dropna(). Drop columns: df.dropna(axis=1). Fill with value: df.fillna(0) or df.fillna(method='ffill') for forward-fill. df['col'].fillna(df['col'].mean()) fills with column mean.