thomas.mac
thomas.mac

Reputation: 1256

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 from a duplicate axis". What I'd like is a df like this:

-0.01   -0.029064
-0.011   -0.032876
-0.012   -0.040795
-0.02   -0.027003
-0.021   -0.031589

(note: added suffixes after each duplicate index)

Upvotes: 2

Views: 1395

Answers (1)

cs95
cs95

Reputation: 402922

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

Upvotes: 3

Related Questions