题解 | #统计运动会项目报名人数#

统计运动会项目报名人数

https://www.nowcoder.com/practice/f992129b6da245d5bc5efb2f95289516

pd.DataFrame.join: Join columns with other DataFrame either on index or on a key column

join(self, other, on=None, how='left', lsuffix='', rsuffix='', sort=False) -> 'DataFrame'

If we want to join using the key columns, we need to set key to be the index in both df and other. The joined DataFrame will have key as its index.

思路:

  1. 先根据两个表的 item_id 字段内连接两个表 join(on='item_id', how='inner')(注意到题目要求只输出报名人数不为0的项目及其对应的报名人数,因此使用内连接)
  2. 再进行分组聚合统计各个项目的人数 groupby('item_name')['employee_id'].count()
import pandas as pd
signup = pd.read_csv('signup.csv')
items = pd.read_csv('items.csv')
df = items.set_index('item_id').join(signup.set_index('item_id'), 
                                     on='item_id', how='inner')
print(df.groupby('item_name')['employee_id'].count())
全部评论
方法不错,没有进行“报名人数不为0”的数据筛选
点赞 回复 分享
发布于 2023-05-27 09:45 山东

相关推荐

11-13 14:37
门头沟学院 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务