多指标系列成pandas数据帧

时间:2018-01-16 14:32:42

标签: pandas aggregation

我有一个有两个指数的熊猫系列:

df_agg=df.groupby(['yearID','teamID']).sum()['Salary']
df_agg.head()

yearID  teamID
1985    ATL       14807000
        BAL       11560712
        BOS       10897560
        CAL       14427894
        CHA        9846178

我想将int转换为像

这样的pandas数据框
yearID     teamID    Salary
1985        ATL       14807000
1985        BAL       11560712
1985        BOS       10897560
1985        CAL       14427894
1985        CHA        9846178

我厌倦了使用:

df_new=df_agg.reset_index(inplace=True)

但我收到以下错误:

TypeError Traceback(最近一次调用最后一次)  in() ----> 1 df_new = df_agg.reset_index(inplace = True)

C:\ Users \ ameimand \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ pandas \ core \ series.py in reset_index(self,level,drop,name,inplace)     966

    index=new_index).__finalize__(self)
    967         elif inplace:
--> 968             raise TypeError('Cannot reset_index inplace on a Series '
    969                             'to create a DataFrame')
    970         else:

TypeError: Cannot reset_index inplace on a Series to create a DataFrame

1 个答案:

答案 0 :(得分:3)

我认为有两个很好的解决方案参数as_index=False

df_new = df.groupby(['yearID','teamID'], as_index=False)['Salary'].sum()

reset_index没有inplace=True

df_new = df.groupby(['yearID','teamID'])['Salary'].sum().reset_index()

<强> 通知

更好的是在groupby [] ['Salary']之后指定聚合列,如df.groupby(['yearID','teamID'], as_index=False)['Salary']

df.groupby(['yearID','teamID']).sum()['Salary']

为:

Salary

因为这会聚合所有列,然后只选择ItemsControl