繁体   English   中英

Pandas groupby()在一列上,然后在另一列上求和

[英]Pandas groupby() on one column and then sum on another

我有一个包含多个列的数据框,但我感兴趣的有三个。这些是nameyeargoals_scored 这些列中没有一个是唯一的,例如我有如下所示的行:

Name           Year     Goals_scored
John Smith     2014     3
John Smith     2014     2
John Smith     2014     0
John Smith     2015     1
John Smith     2015     1
John Smith     2015     2
John Smith     2015     1
John Smith     2015     0
John Smith     2016     1
John Smith     2016     0

我想要做的是创建一个新的数据框,我有4列。 一个用于名称,然后用于2014年,2015年和2016年的每一个。最后三列是相关年份的目标总和的总和。 所以使用上面的数据看起来像:

Name          2014     2015     2016
John Smith    5        5        1

为了使情况变得更糟,他们只希望它包括那些有三年的东西的名字。

谁能指出我正确的方向?

需要groupbysum和重新unstack

df = df.groupby(['Name','Year'])['Goals_scored'].sum().unstack()
print (df)
Year        2014  2015  2016
Name                        
John Smith     5     5     1

替代pivot_table

df = df.pivot_table(index='Name',columns='Year', values='Goals_scored', aggfunc='sum')
print (df)
Year        2014  2015  2016
Name                        
John Smith     5     5     1

最后一个索引列:

df = df.reset_index().rename_axis(None, 1)
print (df)
         Name  2014  2015  2016
0  John Smith     5     5     1

暂无
暂无

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

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