Home > Mobile >  Dataframe reindexing in order
Dataframe reindexing in order

Time:11-27

I have a dataframe like this

   datasource   datavalue
0  aaaa.pdf     5
0  bbbbb.pdf    5
0  cccc.pdf     9

I don't know if this is the reason but this seems to be messing a dash display so I would like to reindex it like

   datasource   datavalue
0  aaaa.pdf     5
1  bbbbb.pdf    5
2  cccc.pdf     9

I used

data_all.reset_index()

but it is not working, the index are still 0

how it should be done?

EDIT1: Thanks to the two participants who made me notice my mistake. I should have put

data_all=data_all.reset_index()

Unfortunately it did not go as expected.

Before:

   datasource   datavalue
0  aaaa.pdf     5
0  bbbbb.pdf    5
0  cccc.pdf     9

Then

data_all.keys()
Index(['datasource','datavalue'],dtype='object')

So data_all.reset_index()

After

  index   datasource   datavalue
0   0  aaaa.pdf     5
1   0  bbbbb.pdf    5
2   0  cccc.pdf     9

data_all.keys() Index(['index','datasource','datavalue'],dtype='object')

As you see one column "index" was added. I suppose I can drop that column but I was expecting something that in one step reindex the df without adding anything

EDIT2: Turns out drop=True was necessary! Thanks everybody!

CodePudding user response:

I think this is what you are looking for.

df.reset_index(drop=True, inplace=True)
#drop: Do not try to insert index into dataframe columns. This resets the index to the default integer index.
# inplace: Whether to modify the DataFrame rather than creating a new one.

CodePudding user response:

Try:

data_all = data_all.reset_index(drop=True)
  • Related