Python 熊猫:如何透视多个列并计算它们的总和?
我有这样一个数据帧:Python 熊猫:如何透视多个列并计算它们的总和?,python,pandas,Python,Pandas,我有这样一个数据帧: Team Player Goals YellowCards RedCards Team1 Player1 2 1 1 Team1 Player2 3 1 0 Team2 Player3 2 2
Team Player Goals YellowCards RedCards
Team1 Player1 2 1 1
Team1 Player2 3 1 0
Team2 Player3 2 2 1
我试图计算每个团队的目标
、黄牌
和红牌
的总和,并为结果创建新的数据框架。我试过:
pd.crosstab(df['Team'],[df['Goals'],df['YellowCards'],df['RedCards']], aggfunc='sum')
但它不起作用。我最好使用crosstab
或pivot\u table
功能来实现这一点。非常感谢您的建议。因为最简单的解决方案是:
df = df.pivot_table(index='Team',aggfunc='sum')
print (df)
Goals RedCards YellowCards
Team
Team1 5 1 2
Team2 2 1 2
像聚合sum
一样工作:
df = df.groupby('Team').sum()
编辑:如果需要,请指定列:
df = df.pivot_table(index='Team',aggfunc='sum',values=['Goals','RedCards','YellowCards'])
print (df)
Goals RedCards YellowCards
Team
Team1 5 1 2
Team2 2 1 2
工作方式如下:
df = df.groupby('Team')[['Goals','RedCards','YellowCards']].sum()
我添加了列总数和总计
data=[('Team1','Player1', 2, 1, 1),
('Team1','Player2', 3, 1, 0),
('Team2','Player3', 2, 2, 1)]
df=pd.DataFrame(data=data,columns=['Team','Player','Goals', 'YellowCards','RedCards'])
fp=df.pivot_table(index='Team',aggfunc='sum')
fp['Totals'] = fp.sum(axis='columns')
fp.loc[('Grand Total'), :] = fp.sum()
print(fp)
输出
Goals RedCards YellowCards Totals
Team
Team1 5.0 1.0 2.0 8.0
Team2 2.0 1.0 2.0 5.0
Grand Total 7.0 2.0 4.0 13.0
使用
df.groupby('Team').sum()
这似乎可行,但如果我有多个包含非整数值的列呢?然后我只需要选择某些列。不幸的是,给@Anurag的values属性提供一个索引列表是不起作用的。@Mr.Engineer-答案已经编辑好了。非常感谢:)