这是一个 df:
-0.01 -0.029064
-0.01 -0.032876
-0.01 -0.040795
-0.02 -0.027003
-0.02 -0.0315
需要将其与另一个框架连接,但出现错误“无法从重复轴重新索引”。我想要的是这样的 df:
-0.01 -0.029064
-0.011 -0.032876
-0.012 -0.040795
-0.02 -0.027003
-0.021 -0.031589
(注意:在每个重复索引后添加后缀)
要获得这些累积计数,请使用groupby
+ cumcount
.
v = df.groupby(df.index)\
.cumcount()\
.astype(str)\
.str.replace('0', '')\
.values
v
array(['', '1', '2', '', '1'], dtype=object)
与索引连接:
df.index = (df.index.values.astype(str) + v).astype(float)
df
Value
-0.010 -0.029064
-0.011 -0.032876
-0.012 -0.040795
-0.020 -0.027003
-0.021 -0.031500
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)