问题描述
现在,我正在对一个DataFrame对象运行求和并对其进行排序:
Right now, I am running a sum and sort on a DataFrame object:
games_tags.groupby(['GameID', 'GameName', 'Tag']).sum().sort(['Count'], ascending=False)
我遇到的问题是,之后,我希望仍然能够通过row ['GameID']等来获取每一行的GameID,GameName和Tag.但是,我注意到在使用总和后()方法,它将创建一个名为"Count"的列,但我无法再访问任何原始列.
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.
我想知道是否有人知道我所缺少的sum()方法的解决方法或某些复杂性.任何帮助表示赞赏.谢谢!
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!
推荐答案
您可以在groupby之后重设索引以将列还原回去:
You can reset the index after the groupby to restore the columns back:
game_tags.reset_index(inplace=True)
这篇关于Pandas DataFrame Sort:要进行汇总和排序,但要保留列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!