Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas reset index after operations on rows

I want to know if there is a way to assign new .loc values to a dataframe in order to index this row. I was writing code where I was indexing rows by the .loc[], but now I have randomly shuffled the dataframe in to two sets and so when I index the row by .loc[], i get a key error as the row might be in the other dataset.

I want to be able to assign a new .loc[] index to the data right after I shuffle so I can still index as I always have.

Example, I have a dataframe:

          length    height...                  water      type
    4     15.85  14.7240  ...               0.173     orange
    92    20.06  17.3565  ...               0.171     orange
    155   22.71  15.8040  ...               0.169     apple
    142   11.76  12.2355  ...               0.175     pear
    91    20.33  16.0785  ...               0.175      apple

The index given is displayed on the left (i.e 4 to 91), I want to change these index values to what I want to assign them, which is in sequential order (i.e 0 to 4). So that when I call .loc[0] it will return the first row and not give me a KeyError as that row is in another dataset

Thanks.

like image 441
Brian Avatar asked Sep 16 '25 00:09

Brian


1 Answers

From Pandas documentation:

>>> df = pd.DataFrame([('bird', 389.0),
...                    ('bird', 24.0),
...                    ('mammal', 80.5),
...                    ('mammal', np.nan)],
...                   index=['falcon', 'parrot', 'lion', 'monkey'],
...                   columns=('class', 'max_speed'))
>>> df
         class  max_speed
falcon    bird      389.0
parrot    bird       24.0
lion    mammal       80.5
monkey  mammal        NaN

using reset_index with drop parameter:

>>> df.reset_index(drop=True)
    class  max_speed
0    bird      389.0
1    bird       24.0
2  mammal       80.5
3  mammal        NaN
like image 160
keramat Avatar answered Sep 18 '25 18:09

keramat