简体   繁体   English

pandas:如何 pivot 多列并计算它们的总和?

[英]pandas: How to pivot multiple columns and calculate their sum?

I have a DataFrame like this:我有一个像这样的 DataFrame:

Team      Player      Goals       YellowCards          RedCards

Team1     Player1       2             1                    1

Team1     Player2       3             1                    0

Team2     Player3       2             2                    1

I'm trying to calculate sum of Goals , YellowCards and RedCards for each team and create new dataframe for result.我正在尝试为每个团队计算GoalsYellowCardsRedCards的总和,并为结果创建新的 dataframe 。 I have tried:我努力了:

pd.crosstab(df['Team'],[df['Goals'],df['YellowCards'],df['RedCards']], aggfunc='sum')

But it's not working.但它不起作用。 Preferably I would like to do this with either crosstab or pivot_table function.最好我想使用crosstabpivot_table function 来执行此操作。 Any advise is highly appreciated.任何建议都受到高度赞赏。

Because need DataFrame.pivot_table the simpliest solution is:因为需要DataFrame.pivot_table最简单的解决方案是:

df = df.pivot_table(index='Team',aggfunc='sum')
print (df)
       Goals  RedCards  YellowCards
Team                               
Team1      5         1            2
Team2      2         1            2

Working like aggregate sum :sum一样工作:

df = df.groupby('Team').sum()

EDIT: If need specify columns:编辑:如果需要指定列:

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

Working like:像这样工作:

df = df.groupby('Team')[['Goals','RedCards','YellowCards']].sum()

I added column totals and grand totals我添加了列总计和总计

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)

output output

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM