简体   繁体   English

重置熊猫数据框中的索引计数

[英]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    |

You can use factorize :您可以使用factorize

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

You could also use @Chris' solution in the comments.您也可以在评论中使用@Chris 的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM