Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame Sort: Want to sum and sort, but keep column names

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!

like image 627
Zhouster Avatar asked Dec 02 '25 10:12

Zhouster


1 Answers

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

game_tags.reset_index(inplace=True)
like image 152
EdChum Avatar answered Dec 04 '25 09:12

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!