Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Reindex Unsorts Dataframe

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!

like image 923
David Yang Avatar asked Sep 18 '13 17:09

David Yang


2 Answers

Instead of reindexing, just change the actual index:

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

Then that wont change the order, just the index

like image 89
Ryan Saxe Avatar answered Nov 15 '22 13:11

Ryan Saxe


I think you're misunderstanding what reindex does. It uses the passed index to select values along the axis passed, then fills with NaN wherever your passed index doesn't match up with the current index. What you're interested in is just setting the index to something else:

In [12]: df = DataFrame(randn(10, 2), columns=['a', 'delt'])

In [13]: df
Out[13]:
       a   delt
0  0.222 -0.964
1  0.038 -0.367
2  0.293  1.349
3  0.604 -0.855
4 -0.455 -0.594
5  0.795  0.013
6 -0.080 -0.235
7  0.671  1.405
8  0.436  0.415
9  0.840  1.174

In [14]: df.reindex(index=arange(1, len(df) + 1))
Out[14]:
        a   delt
1   0.038 -0.367
2   0.293  1.349
3   0.604 -0.855
4  -0.455 -0.594
5   0.795  0.013
6  -0.080 -0.235
7   0.671  1.405
8   0.436  0.415
9   0.840  1.174
10    NaN    NaN

In [16]: df.index = arange(1, len(df) + 1)

In [17]: df
Out[17]:
        a   delt
1   0.222 -0.964
2   0.038 -0.367
3   0.293  1.349
4   0.604 -0.855
5  -0.455 -0.594
6   0.795  0.013
7  -0.080 -0.235
8   0.671  1.405
9   0.436  0.415
10  0.840  1.174

Remember, if you want len(df) to be in the index you have to add 1 to the endpoint since Python doesn't include endpoints when constructing ranges.

like image 27
Phillip Cloud Avatar answered Nov 15 '22 14:11

Phillip Cloud