Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将相似的索引值分组,并用新的索引值对每组重新编制索引_Python_Pandas_Dataframe - Fatal编程技术网

Python 将相似的索引值分组,并用新的索引值对每组重新编制索引

Python 将相似的索引值分组,并用新的索引值对每组重新编制索引,python,pandas,dataframe,Python,Pandas,Dataframe,我有df值和类似的索引,如 >>> df idx a b 0 15 15 0 15 15 0 15 15 1 20 20 1 20 20 1 20 20 1 20 20 1 20 20 而我想给每个组分配单独的索引,如下面的结果。我怎样才能在熊猫中得到这个 分组 >> idx a b 0 15 15 1 15 15 2 15 15 0 20 20 1 20 20 2 20 20 3 20 20 4

我有df值和类似的索引,如

>>> df
idx  a  b
0  15  15
0  15  15
0  15  15
1  20  20
1  20  20
1  20  20
1  20  20
1  20  20
而我想给每个组分配单独的索引,如下面的结果。我怎样才能在熊猫中得到这个

分组

>>
idx  a  b
0  15  15
1  15  15
2  15  15
0  20  20
1  20  20
2  20  20
3  20  20
4  20  20
使用

使用

df.index=df.groupby(df.index).cumcount()
df.index=df.groupby(df.index).cumcount()
>>> df['idx'] = df.groupby('idx').cumcount()
>>> df
   idx   a   b
0    0  15  15
1    1  15  15
2    2  15  15
3    0  20  20
4    1  20  20
5    2  20  20
6    3  20  20
7    4  20  20