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

Sea*_*rdy 4 python matplotlib dataframe pandas

我有一个包含多个列的数据框,但我感兴趣的是三个.这些是name,yeargoals_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
Run Code Online (Sandbox Code Playgroud)

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

Name          2014     2015     2016
John Smith    5        5        1
Run Code Online (Sandbox Code Playgroud)

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

谁能指出我正确的方向?

jez*_*ael 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
Run Code Online (Sandbox Code Playgroud)

另类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
Run Code Online (Sandbox Code Playgroud)

最后一个索引列:

df = df.reset_index().rename_axis(None, 1)
print (df)
         Name  2014  2015  2016
0  John Smith     5     5     1
Run Code Online (Sandbox Code Playgroud)