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

时间:2017-10-17 11:04:54

标签: python pandas matplotlib dataframe

我有一个包含多个列的数据框,但我感兴趣的是三个。这些是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

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

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:5)

需要groupby,汇总sum并按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
相关问题