Pandas Cheat Sheet
Pandas Cheat Sheet
import pandas as pd
df = pd.read_csv("file.csv")
df.shape
df.columns
df["column_name"]
df[["col1", "col2"]]
df[(df["col1"] > 50) & (df["col2"] < 30)] # Multiple conditions (AND)
# Modify a column
df["col1"] = df["col1"] * 10
# Drop columns
df = df.drop(columns=["col1", "col2"])
df = df.drop(index=[0, 1])
# Rename columns
df = df.rename(columns={"old_name": "new_name"})
# Replace values
# Sort by a column
df = df.sort_values(by="col1", ascending=True)
df_grouped = df.groupby("category").agg({
"value1": "sum",
"value2": "mean"
}) # Custom aggregation
6. Combining DataFrames
7. Applying Functions
df["col1"] = df["col1"].apply(lambda x: x * 2)
8. Saving Data
df.to_csv("output.csv", index=False)
df.to_excel("output.xlsx", index=False)
9. Handling Duplicates
# Drop duplicate rows
df = df.drop_duplicates()
df = df.reset_index(drop=True)
df = df.set_index("column_name")