Pandas: Reindex Unsorts Dataframe

David Yang picture David Yang · Sep 18, 2013 · Viewed 13k times · Source

I'm having some trouble sorting and then resetting my Index in Pandas:

dfm = dfm.sort(['delt'],ascending=False)
dfm = dfm.reindex(index=range(1,len(dfm)))

The dataframe returns unsorted after I reindex. My ultimate goal is to have a sorted dataframe with index numbers from 1 --> len(dfm) so if there's a better way to do that, I wouldn't mind,

Thanks!

Answer

Ryan Saxe picture Ryan Saxe · Sep 18, 2013

Instead of reindexing, just change the actual index:

dfm.index = range(1,len(dfm) + 1)

Then that wont change the order, just the index