DEPLOYEDAI-era interview training
RankBootstrapping
XP 0 / 250
0
0%
Ready
Data Analyst & Analytics Interview · Python / pandas for Analysis · Lesson 1 of 2

Cleaning & groupby

7 min

Many analyst roles expect pandas alongside SQL — especially for cleaning messy data and analysis SQL can't easily express. The core loop is load → clean → group → summarize.

import pandas as pd

df = pd.read_csv("orders.csv")

# Clean: types, missing values, duplicates
df["order_date"] = pd.to_datetime(df["order_date"])
df["amount"] = df["amount"].fillna(0)
df = df.drop_duplicates(subset=["order_id"])
df = df[df["amount"] >= 0]                      # drop bad rows

# Group + aggregate (the SQL GROUP BY of pandas)
summary = (
    df.groupby("customer_id")
      .agg(n_orders=("order_id", "count"),
           revenue=("amount", "sum"))
      .reset_index()
      .sort_values("revenue", ascending=False)
)
  • Missing data: isna() to find, fillna() or dropna() to handle — decide per column whether a NULL means zero or unknown.
  • Types: to_datetime / astype; wrong dtypes silently break sorts and math.
  • Dedup: drop_duplicates(subset=...); groupby().agg() mirrors SQL aggregation.
  • Vectorize: prefer column operations over Python for loops or apply — orders of magnitude faster.
Chained assignment never worksSince pandas 3.0 (Copy-on-Write is the only mode), chained indexing like df[df.x>0]['y'] = 1 never modifies the original — it writes to a temporary copy, and the old SettingWithCopyWarning is gone. Always assign through .loc[rows, 'col']. Mentioning the pandas-3 change signals you're current.
pandas is SQL you can composegroupby().agg() = GROUP BY, df[mask] = WHERE, merge() = JOIN, sort_values() = ORDER BY. Map each pandas verb to its SQL clause and the API stops feeling foreign.

◆ Lock it in

  • The loop is load → clean (types, NaN, dupes) → groupby.agg → sort.
  • Decide per column whether missing means zero or unknown.
  • Use .loc for assignment; vectorize instead of looping.
Feynman drill — say it out loudMap four pandas operations (filter, groupby.agg, merge, sort_values) to their SQL equivalents, and explain when a NaN should become 0 vs be dropped.
Step 1 rate your confidence · Step 2 pick your answer
Which pandas operation is the direct equivalent of SQL's GROUP BY ... aggregate?
Step 1 — how sure are you?