Several summaries and two-level groups
count, size, agg with many functions, and grouping by two columns.
Sometimes one summary per group is not enough. .agg(["sum", "mean", "count"]) computes several at once and returns a small table: one row per group, one column per summary. count() counts the non-missing values in a column, while size() counts rows, missing or not. You can also group by two columns, like groupby(["store", "item"]), to get one answer for every store-and-item pair.
import io
import pandas as pd
csv_text = """day,store,item,units,price
Mon,North,Coffee,30,3.5
Mon,North,Muffin,12,2.75
Mon,South,Coffee,22,3.5
Mon,South,Tea,9,3.0
Tue,North,Coffee,35,3.5
Tue,North,Tea,7,3.0
Tue,South,Coffee,18,3.5
Tue,South,Muffin,15,2.75
Wed,North,Muffin,10,2.75
Wed,South,Coffee,27,3.5
"""
sales = pd.read_csv(io.StringIO(csv_text))
summary = sales.groupby("store")["units"].agg(["sum", "mean", "count"])
print(summary)
print(summary.loc["North", "mean"])
pairs = sales.groupby(["store", "item"])["units"].sum()
print(pairs)
print(pairs.loc[("North", "Coffee")])summary is a table with columns sum, mean and count. The two-column groupby has a two-level index, so you read one value with a pair: ("North", "Coffee") gives 65.
Add .reset_index() to turn the group labels back into ordinary columns, which is handy before charting or merging.