Andrew
Andrew

Reputation: 714

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

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

Upvotes: 1

Views: 371

Answers (2)

user7864386
user7864386

Reputation:

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

Upvotes: 1

eshirvana
eshirvana

Reputation: 24568

Here is one way:

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

Upvotes: 2

Related Questions