Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Index Count in a pandas dataframe

I have a dataframe with multiple rows per index and want to reset the count of the index but keeping the same multiple rows per index.

Example:

     | Name |
1234 |  AA  |
1234 |  AB  |
1235 |  BA  |
1235 |  BB  |
1236 |  CA  |
1236 |  CB  |
1237 |  DA  |

I'm trying to have something such as:

  |  Name   |
1 |   AA    |
1 |   AB    |
2 |   BA    |
2 |   BB    |
3 |   CA    |
3 |   CB    |
4 |   DA    |
like image 388
rekeson21 Avatar asked May 17 '26 18:05

rekeson21


1 Answers

You can use factorize:

df.index = df.index.factorize()[0] + 1

You could also use @Chris' solution in the comments.

like image 131
sammywemmy Avatar answered May 20 '26 06:05

sammywemmy