Cheat sheets
Pandas cheat sheet
Quick reference for the most-used pandas commands to load, explore, clean, and transform DataFrames.
Load and explore data
pd.read_csv('file.csv')- Loads a CSV into a DataFrame.
df.head(n)- Shows the first n rows (5 by default).
df.info()- Column types, nulls, and memory usage.
df.describe()- Summary statistics for numeric columns.
df.shape- Tuple of (rows, columns).
df.columns- List of column names.
df.dtypes- Data type of each column.
Select and filter
df['column']- Selects one column as a Series.
df[['a', 'b']]- Selects several columns as a DataFrame.
df.loc[row, 'column']- Label-based row/column selection.
df.iloc[0, 1]- Integer-position-based selection.
df[df['age'] > 18]- Filters rows with a boolean condition.
df.query("age > 18")- Filters with a text expression.
Clean data
df.isna().sum()- Counts missing values per column.
df.dropna()- Drops rows with any missing value.
df.fillna(0)- Fills missing values with a fixed value.
df.drop_duplicates()- Removes duplicate rows.
df.astype({'col': 'int'})- Converts one or more columns' type.
df.rename(columns={'a': 'b'})- Renames columns.
Group and combine
df.groupby('col').sum()- Groups by column and sums each group.
df.groupby('col').agg(['sum', 'mean'])- Applies several aggregations at once.
df.sort_values('col', ascending=False)- Sorts by a column.
pd.merge(df1, df2, on='id', how='left')- Joins two DataFrames on a key column.
pd.concat([df1, df2])- Stacks DataFrames on top of each other.
df.pivot_table(index='a', columns='b', values='c')- Pivot table.