Skip to content Skip to sidebar Skip to footer

Adding Suffix To Duplicate Index Values

Here is a df: -0.01 -0.029064 -0.01 -0.032876 -0.01 -0.040795 -0.02 -0.027003 -0.02 -0.0315 Need to concat this with another frame, but get the error 'cannot reindex fro

Solution 1:

To get these cumulative counts, use groupby + cumcount.

v = df.groupby(df.index)\
      .cumcount()\
      .astype(str)\
      .str.replace('0', '')\
      .values

v 
array(['', '1', '2', '', '1'], dtype=object)

Concatenate with the index:

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

Post a Comment for "Adding Suffix To Duplicate Index Values"