Pandas: Reindex Unsorts Dataframe

13,130

Solution 1

Instead of reindexing, just change the actual index:

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

Then that wont change the order, just the index

Solution 2

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.

Share:
13,130

Related videos on Youtube

David Yang
Author by

David Yang

Updated on August 31, 2022

Comments

  • David Yang
    David Yang over 1 year

    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!

  • BrenBarn
    BrenBarn over 10 years
    Like most answers about reindex, this again shows how terrible a name reindex is for what this method does.
  • Phillip Cloud
    Phillip Cloud over 10 years
    @BrenBarn Eh, maybe. What would you have called it?
  • Andy Hayden
    Andy Hayden over 10 years
    @BrenBarn I have a feeling it may get worse with set_index :S tricky.
  • BrenBarn
    BrenBarn over 10 years
    @PhillipCloud: I would call it something like get, because all it does is get elements based on their existing index values. reindex implies you are setting a new index for existing values.
  • Robert
    Robert almost 10 years
    The Pandas reindex function does something like Excel's VLOOKUP.
  • kztd
    kztd about 7 years
    Piling on to Ryan's answer: df.index = df.index.sort_values()