题解 | #运动会各项目报名透视表#
运动会各项目报名透视表
https://www.nowcoder.com/practice/dc8ea5c6572243d1a38c907d115cfb47
模拟 数据(连这数据我都模拟出来了)
signup.csv
employee_id,name,sex,department,item_id NC001,liming,f,functional,N100 NC002,zhangsan,f,functional,N101 NC003,sunli,m,functional,N103 NC004,lina,m,functional,N103 NC005,wanger,m,technology,N101 NC006,shenteng,m,education,N104 NC007,mali,f,operation,N102 NC008,reba,f,technology,N100 NC009,ailun,m,education,N101 NC010,changyuan,f,technology,N102 NC011,geyou,m,operation,N102 NC012,cls,f,technology,N102 NC013,zhangwei,m,education,N102 NC014,liufang,m,education,N102 NC015,wanglei,f,education,N104 NC016,zhangli,f,education,N101 NC017,wangyan,f,operation,N103 NC018,liangfeifan,f,operation,N103
items.csv
item_id,item_name,location N100,athletics,area01 N101,longJump,area02 N102,highJump,area03 N103,javelin,area04 N104,pingPong,area05 N105,relayRace,area06
实现代码
import pandas as pd signup = pd.read_csv("signup.csv") items = pd.read_csv("items.csv") df_all = pd.merge( signup, items, how="left", left_on=signup["item_id"], right_on=items["item_id"] ) print( pd.pivot_table( df_all, index=["sex", "department"], columns=["item_name"], values=["employee_id"], aggfunc="count", fill_value=0, ) )