从一堆行里提炼群体差异,再把分散信息拼回完整语境
Pandas:分组、透视与连接
- 用 Split–Apply–Combine 解释 groupby 的计算过程
- 使用 agg 和 pivot_table 生成可比较的汇总表
- 根据键和保留范围选择合适的 merge 方式
本讲要解决的问题
逐行查看数据很难回答群体问题:“哪个区域骑行量最高?”“周末和工作日的模式是否不同?”“站点容量与需求是否匹配?”这类问题需要先分组,再对每组应用同一个统计量。
import pandas as pd
daily = pd.DataFrame({
"station_id": ["S01", "S02", "S03", "S04", "S05"],
"district": ["北区", "南区", "北区", "西区", "南区"],
"day_type": ["工作日", "工作日", "周末", "工作日", "周末"],
"rides": [128, 94, 173, 81, 116],
"shortage_minutes": [18, 7, 24, 5, 13],
})1. GroupBy 是 Split–Apply–Combine
执行 groupby 时,可以把过程想成三步:
- Split:按照键把行分到不同桶里。
- Apply:在每个桶上执行同一种计算。
- Combine:把每组结果拼成新的 Series 或 DataFrame。
观察 Split → Apply → Combine
北区128
南区94
北区173
西区81
南区116
北区128 + 173
南区94 + 116
西区81
北区301
南区210
西区81
df.groupby('district')['rides'].sum()district_rides = daily.groupby("district")["rides"].sum()
print(district_rides)groupby("district") 本身只是描述分组规则;直到调用 .sum(),Pandas 才知道每组要留下什么结果。
2. agg 一次生成多种统计量
summary = (
daily
.groupby("district")
.agg(
station_count=("station_id", "nunique"),
total_rides=("rides", "sum"),
average_rides=("rides", "mean"),
max_shortage=("shortage_minutes", "max"),
)
.reset_index()
)
print(summary)命名聚合把“输出列名、输入列、聚合函数”写在一起。结果比先计算四张表再拼接更容易审查。
.size() 和 .count() 不完全相同
.size()计算每组行数,包括字段中存在缺失值的行。.count()对每一列计算非缺失值数量。
如果你要问“这一组有多少条记录”,优先考虑 .size();如果问“这个字段有多少个有效观测”,使用 .count()。
CHECKPOINT
`daily.groupby('district')` 已经完成了什么?
3. 多列分组与透视表
按区域和日期类型同时分组:
by_district_day = (
daily
.groupby(["district", "day_type"], as_index=False)
.agg(total_rides=("rides", "sum"))
)
print(by_district_day)长表适合继续计算;透视表适合把一个分类变量展开为列,便于比较:
pivot = daily.pivot_table(
index="district",
columns="day_type",
values="rides",
aggfunc="mean",
fill_value=0,
)
print(pivot)4. Join:通过键把不同表的语境拼起来
骑行日报记录需求,站点表记录容量和区域属性。两张表通过 station_id 连接:
stations = pd.DataFrame({
"station_id": ["S01", "S02", "S03", "S04", "S06"],
"station_name": ["湖畔", "剧场", "大学城", "体育馆", "火车北站"],
"capacity": [20, 16, 24, 15, 30],
})
merged = daily.merge(stations, on="station_id", how="left", validate="many_to_one")
print(merged)这里选择 left,因为分析主体是 daily 的骑行记录:即使某个站点在站点表中缺少信息,也要保留记录并暴露缺失值。
validate="many_to_one" 把业务假设写进代码:日报中一个站点可以出现多次,但站点表中每个 station_id 应只出现一次。如果右表键重复,连接会把行意外倍增,验证可以尽早报错。
常见连接方式
how | 保留范围 | 典型问题 |
|---|---|---|
inner | 两边键都出现的行 | 哪些记录拥有完整匹配信息? |
left | 左表全部行 | 为主分析表补充属性时,哪些没有匹配? |
right | 右表全部行 | 是否覆盖了完整参考清单? |
outer | 两边所有键 | 两张清单有哪些差异? |
print("连接前:", len(daily))
print("连接后:", len(merged))
print("未匹配站点:", merged.loc[merged["station_name"].isna(), "station_id"].tolist())5. 从汇总表回到业务问题
汇总不是终点。假设北区总骑行量最高,至少有两种解释:北区站点更多,或单站需求更高。因此要同时查看 station_count、total_rides 和 average_rides。
report = (
merged
.groupby("district", as_index=False)
.agg(
stations=("station_id", "nunique"),
total_rides=("rides", "sum"),
average_capacity=("capacity", "mean"),
total_shortage_minutes=("shortage_minutes", "sum"),
)
.sort_values("total_shortage_minutes", ascending=False)
)
print(report)6. 知识检查
CHECKPOINT
要计算每个区域的记录行数,并且缺失值行也要计入,应优先使用什么?
CHECKPOINT
透视表中同一个 district 与 day_type 组合有多行时,为什么需要 aggfunc?
CHECKPOINT
以日报表为分析主体,为它补充站点容量,同时保留所有日报记录,应该使用哪种连接?
CHECKPOINT
连接后行数意外翻倍,最可能的原因是什么?
CHECKPOINT
某区域 total_rides 最高,为什么不能立刻断言单站需求最高?
7. 代码练习:构造区域调度报告
使用本讲的 daily 和 stations:
- 左连接两张表,并验证关系是
many_to_one。 - 找出未匹配的
station_id。 - 按区域计算站点数、总骑行量、平均容量和总缺车分钟。
- 新增
shortage_per_station。 - 按该指标从高到低排列。
提示
聚合后再计算 shortage_per_station = total_shortage_minutes / stations,避免在行级重复除法后错误汇总。
参考实现
import pandas as pd
daily = pd.DataFrame({
"station_id": ["S01", "S02", "S03", "S04", "S05"],
"district": ["北区", "南区", "北区", "西区", "南区"],
"day_type": ["工作日", "工作日", "周末", "工作日", "周末"],
"rides": [128, 94, 173, 81, 116],
"shortage_minutes": [18, 7, 24, 5, 13],
})
stations = pd.DataFrame({
"station_id": ["S01", "S02", "S03", "S04", "S06"],
"station_name": ["湖畔", "剧场", "大学城", "体育馆", "火车北站"],
"capacity": [20, 16, 24, 15, 30],
})
merged = daily.merge(stations, on="station_id", how="left", validate="many_to_one")
unmatched = merged.loc[merged["station_name"].isna(), "station_id"].tolist()
report = (
merged
.groupby("district", as_index=False)
.agg(
stations=("station_id", "nunique"),
total_rides=("rides", "sum"),
average_capacity=("capacity", "mean"),
total_shortage_minutes=("shortage_minutes", "sum"),
)
)
report["shortage_per_station"] = report["total_shortage_minutes"] / report["stations"]
report = report.sort_values("shortage_per_station", ascending=False)
print("未匹配:", unmatched)
print(report)8. 常见错误
- 分组前没有明确每组最终需要留下什么统计量。
- 用
.count()计算行数,却忽略该列中的缺失值。 - 透视表没有明确重复组合的聚合规则。
- 连接前不检查键是否唯一,连接后不检查行数。
- 用总量直接比较规模不同的群体。
9. 本讲小结
groupby的本质是 Split–Apply–Combine。- 命名聚合让输入列、函数和输出列更容易审查。
- 透视表重新排列并聚合信息,适合比较但会压缩细节。
merge必须明确连接键、保留范围和预期键关系。
参考资料
- Berkeley Data 100 Summer 2025,Lecture 04:分组、透视和连接的知识边界。
- Learning Data Science,Ch.6:聚合与连接主题。
- 本页案例、文字、图解、代码、问题和练习均为本课程原创。