将多重索引序列转换成pandas数据框。

5

我有一个带有两个索引的pandas系列:

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)

但是我收到了以下错误:


类型错误 追踪 (最近的调用在最上面) in () ----> 1 df_new=df_agg.reset_index(inplace=True)

在C:\Users\ameimand\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\series.py中,重置索引时出现了问题。您可以尝试修改参数 level、drop 或 name,或者使用 inplace 参数来直接修改原始数据。

    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个回答

8
我认为有两种不错的解决方案,参数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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接