Home > Net >  Unify equal values ​in different column in pandas?
Unify equal values ​in different column in pandas?

Time:04-19

Suppose I have a column like so:

COLUMN A     
abc           
dbe        
dbe        
abc        
abc        
ajk        
dbe        
abc        

I expected the follow output:

KEY  
abc1   
dbe1
dbe2
abc2
abc3
ajk1
dbe3
abc4

The point is to give uniqueness to each duplicate.

Any idea?

Thank you in advance.

CodePudding user response:

Try this:

df['key'] = df.groupby('COLUMNA').cumcount().add(1)
df['key'] = df['COLUMNA']   df['key'].astype(str)
print(df)

  COLUMNA   key
0     abc  abc1
1     dbe  dbe1
2     dbe  dbe2
3     abc  abc2
4     abc  abc3
5     ajk  ajk1
6     dbe  dbe3
7     abc  abc4
  • Related