Pandas Cheat Sheet
2 min readNov 21, 2024
Here’s a Pandas Cheat Sheet summarizing essential functionalities for quick reference.
1. Importing Pandas
import pandas as pd2. Creating Data
- Create a Series
- Create a DataFrame
- Read Data
- Write Data
1 # Create a Series
data = pd.Series([1, 2, 3, 4], name="numbers")
2 #Create a DataFrame
data = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "San Francisco", "Los Angeles"]
})
3 #Read Data
df = pd.read_csv("file.csv") # From a CSV file
df = pd.read_excel("file.xlsx") # From an Excel file
df = pd.read_json("file.json") # From a JSON file
4 # Write Data
df.to_csv("file.csv", index=False) # Write to a CSV file
df.to_excel("file.xlsx", index=False) # Write to an Excel file
df.to_json("file.json", orient="records") # Write to a JSON file3. Viewing Data
df.head(n) # First n rows (default 5)
df.tail(n) # Last n rows (default 5)
df.info() # Summary of DataFrame
df.describe() # Descriptive stats (numerical columns)
df.shape # Rows and columns
df.columns # Column names
df.index # Index labels4. Selecting Data
- Select Columns
- Select Rows
- Filter Rows
1 #Select Columns
df["column_name"] # Single column
df[["col1", "col2"]] # Multiple columns
2 #Select Rows
df.iloc[0] # First row by position
df.loc[0] # First row by index label
df.loc[0:2, "col1"] # Range of rows and specific column
3 # Filter Rows
df[df["Age"] > 30] # Rows where Age > 30
df[df["City"] == "New York"] # Rows where City = "New York"Get Arpita Pal’s stories in your inbox
Join Medium for free to get updates from this writer.
5. Adding/Modifying Columns
df["New_Column"] = df["Age"] * 2 # Create or modify a column
df["Category"] = df["Age"].apply(lambda x: "Adult" if x > 18 else "Minor")6. Dropping Data
df.drop("column_name", axis=1, inplace=True) # Drop column
df.drop([0, 1], axis=0, inplace=True) # Drop rows7. Sorting
df.sort_values(by="Age", ascending=True, inplace=True) # Sort by column
df.sort_index(ascending=False, inplace=True) # Sort by index8. Aggregations
df["Age"].mean() # Mean of Age
df["Age"].sum() # Sum of Age
df["Age"].max() # Maximum Age
df["Age"].min() # Minimum Age
df.groupby("City").mean() # Group by City and calculate mean9. Handling Missing Data
df.isnull().sum() # Check missing values
df.dropna(inplace=True) # Drop rows with missing values
df.fillna(0, inplace=True) # Fill missing values with 010. Merging/Joining DataFrames
- Concatenate
- Merge
#Concatenate
result = pd.concat([df1, df2], axis=0) # Vertical concat (rows)
result = pd.concat([df1, df2], axis=1) # Horizontal concat (columns)
# Merge
result = pd.merge(df1, df2, on="key", how="inner") # Join on "key" column11. Pivot Table
df.pivot_table(values="Age", index="City", columns="Gender", aggfunc="mean")12. Exporting Data
df.to_csv("output.csv", index=False) # Export to CSV
df.to_excel("output.xlsx", index=False) # Export to Excel
df.to_json("output.json", orient="records") # Export to JSON13. Other Useful Functions
df.sample(n=5) # Random sample of rows
df.nunique() # Number of unique values per column
df.duplicated() # Check for duplicate rows
df.corr() # Correlation between columns
df.memory_usage() # Memory usage of DataFrame




