完成一对多合并
繁琐的准备工作已经完成,现在该合并这些表了。您将把 game_matchups 与 punts 进行连接。您或许还记得,此前您已经确认这些数据框可以直接使用。接下来只需快速查看以唤起记忆,然后编写代码。
合并数据后,我们可以通过按 GameKey 分组,并统计 PlayId 列中的条目数量,来确定每场比赛的弃踢(punt)次数。相关代码已为您提供。
本练习是课程的一部分
面向表格用户的 pandas 连接
练习说明
- 查看每个数据框的前 5 行。
- 执行内连接,使用
punts作为右侧数据框,并查看结果
交互式实操练习
通过完成这段示例代码来试试这个练习。
# View first 5 rows of each data frame
print(____.head(), '\n', ____.head())
# Merge data frames
games_all = ____.merge(____, how='____')
print(____.head(10))
# Produce counts of games by number of punts
counts = games_all.groupby('GameKey')['PlayId'].size()
counts.hist()
plt.xlabel("Punts per Game")
plt.ylabel("Number of Games")
plt.show()