Pandas DataFrame Sort:要进行汇总和排序,但要保留列名 [英] Pandas DataFrame Sort: Want to sum and sort, but keep column names

查看:100
本文介绍了Pandas DataFrame Sort:要进行汇总和排序,但要保留列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在对一个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:要进行汇总和排序,但要保留列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆