如果这是数据帧
VisitorID visitNumber Country
1 1 USA
2 1 UK
3 1 CANADA
3 2 CANADA
4 1 MEXICO
我想用matplotlib绘制关于每个国家的游客的piechart(因此每个国家的游客数量为33%),所以我不想将加拿大计算两次(因为它具有相同的VisitorID)
我已经找了好几个小时了,还是找不到答案。
我试过:
df2 = df.groupby('VisitorID').agg({'visitNumber': 'max'}).reset_index()
正在删除其他列,我甚至看不到形状了如果我试着运行:
df2.shape()
输出为:
TypeError: 'tuple' object is not callable
您也可以为Country
指定聚合函数:
df2 = df.groupby('VisitorID').agg({'visitNumber': 'max', 'Country': 'first'}).reset_index()
另外shape
是一个属性,而不是一个方法。所以去掉括号:
df2.shape