Nexus Learn

Log in · Create free account

Module 4: Data analysis with pandas · 12 min

Filter, group and sort

big = df[df["sales"] > 200]            # filter rows
print(big)

print(df["sales"].mean())                   # average

by_city = df.groupby("city")["sales"].sum()   # total per city
print(by_city)

print(df.sort_values("sales", ascending=False))   # biggest first

df["sales_k"] = df["sales"] / 1000             # new column

Think of groupby as: split the table into groups, calculate something for each group, then put the answers together.

Always look at your data first. Wrong results usually come from surprises in the data, not from the code.

(free account needed to save progress)

← Loading data with pandas  |  Cleaning messy data →