Home > Enterprise >  How to rename the same value in a column using pandas?
How to rename the same value in a column using pandas?

Time:12-30

My dataframe is : enter image description here

How can I display rows with the same data in the hospital column in this way via pandas?

enter image description here

CodePudding user response:

Here is one way:

df['hospital']  = '_'   df.groupby('hospital').cumcount().add(1).map(str)

CodePudding user response:

You can groupby Hospital and transform a function that counts the number of hospitals in each group and adds a cumulative index if that number exceeds 1 and adds nothing if its a singleton:

df['Hospital'] = df['Hospital']   df.groupby('Hospital')['Hospital'].transform(lambda x: '_'   (x==x).cumsum().astype(str) if len(x)>1 else '')

Output:

                           Hospital
0               UCLA Medical Center
1           Massachusetts General_1
2           Massachusetts General_2
3  Northwestern Memorial Hospital_1
4  Northwestern Memorial Hospital_2
5  Northwestern Memorial Hospital_3
6              Mount Sinai Hospital
  • Related