我有这样一个数据帧:
Team Player Goals YellowCards RedCards
Team1 Player1 2 1 1
Team1 Player2 3 1 0
Team2 Player3 2 2 1
我试图计算每个团队的Goals
、YellowCards
和RedCards
之和,并为结果创建新的数据框架。我试过:
pd.crosstab(df['Team'],[df['Goals'],df['YellowCards'],df['RedCards']], aggfunc='sum')
但它不起作用。我最好使用crosstab
或pivot_table
函数来实现这一点。任何建议都将不胜感激
我添加了列总数和总计
输出
因为需要^{} ,最简单的解决方案是:
像聚合一样工作
sum
:编辑:如果需要,请指定列:
工作方式如下:
相关问题 更多 >
编程相关推荐