Pandas DataFrame Sort: Want to sum and sort, but k

2019-07-01 10:07发布

Right now, I am running a sum and sort on a DataFrame object:

games_tags.groupby(['GameID', 'GameName', 'Tag']).sum().sort(['Count'], ascending=False)

The issue I'm running into is that afterwards, I want to be able to still grab each row's GameID, GameName, and Tag via row['GameID'], etc. However, I noticed that after I use the sum() method, it creates a column named 'Count', but I can no longer access any of the original columns.

I was wondering if anyone knows a work around or some intricacy to the sum() method that I am missing. Any help is appreciated. Thanks!

1条回答
【Aperson】
2楼-- · 2019-07-01 10:55

You can reset the index after the groupby to restore the columns back:

game_tags.reset_index(inplace=True)
查看更多
登录 后发表回答