Pandas Tutorial
Wrangle tabular data with Pandas — DataFrames, filtering, grouping, and joins.
Pandas is the go-to library for tabular data in Python. This tutorial covers the operations you’ll use daily.
Loading data
import pandas as pd
df = pd.read_csv("sales.csv")
print(df.head())
print(df.shape) # (rows, columns)
Selecting and filtering
df["price"] # a column (Series)
df[df["price"] > 100] # rows where price > 100
df.loc[df["region"] == "EU", ["product", "price"]]
Grouping
agg = df.groupby("region")["price"].agg(["sum", "mean", "count"])
print(agg.sort_values("sum", ascending=False))
Handling missing data
df.dropna(subset=["price"]) # drop rows missing price
df["price"].fillna(df["price"].mean(), inplace=True)
Joining
orders = pd.read_csv("orders.csv")
users = pd.read_csv("users.csv")
merged = orders.merge(users, on="user_id", how="left")
| Method | SQL analog |
|---|---|
merge |
JOIN |
concat |
UNION / stacking rows |
groupby |
GROUP BY |
Use vectorized operations (whole-column) instead of Python loops — they’re 10–100x faster.