Pandas Cheat Sheet
Let me give you a quick reference for the most common Pandas operations. Bookmark this page — you'll come back to it constantly.
Reading & Writing Data
df = pd.read_csv('file.csv')
df = pd.read_excel('file.xlsx')
df = pd.read_json('file.json')
df.to_csv('output.csv', index=False)
df.to_excel('output.xlsx', index=False)
Quick Overview
df.head() # First 5 rows
df.tail() # Last 5 rows
df.info() # Column types, non-null counts
df.describe() # Statistical summary
df.shape # (rows, columns)
df.columns # Column names
df.dtypes # Data types per column
Selection
df['column'] # Single column
df[['col1', 'col2']] # Multiple columns
df.iloc[0:5] # Rows by position
df.loc[0:5] # Rows by label
df[df['col'] > 10] # Conditional filter
Data Cleaning
df.dropna() # Remove missing values
df.fillna(0) # Fill missing with 0
df.drop_duplicates() # Remove duplicates
df.rename(columns={'old': 'new'})
df['col'] = df['col'].astype(int)
Aggregation & Merging
df.groupby('col').agg({'val': 'mean'})
pd.merge(df1, df2, on='key')
pd.concat([df1, df2]) # Stack DataFrames
One thing that confused me at first was the difference between `merge` and `concat`. `merge` is for combining on a key column (like SQL joins). `concat` is for stacking DataFrames on top of each other.
Try it Yourself →Key Takeaways
- Keep this cheat sheet handy for quick reference
- Reading/writing data is just one function call away
- Use `info()` and `describe()` for quick data overviews
- Master selection, cleaning, and aggregation to handle any data task