Home > Software engineering >  Rename equal rows in same column
Rename equal rows in same column

Time:08-18

i have this dataframe:

enter image description here

What i want is too loop over the 'labels' column, and if some label have the same name, i want to change it, by adding a incremental variable. So the result i want is something like this:

 clusters            labels
 Cluster 0       price range
 Cluster 1   going cambridge
 Cluster 2  reference number
 Cluster 3  reference number 1
 Cluster 4   going cambridge 1
 Cluster 5      include free
 Cluster 6      british food
 Cluster 7       center town
 Cluster 8          car type
 Cluster 9        need place
 Cluster 10  reference number 2

CodePudding user response:

Use groupby, cumcount and str.cat:

df["labels"] = df["labels"].str.cat(
    df.groupby("labels").cumcount().astype(str).str.replace("0", ""), sep = " "
)

output:

      clusters              labels
0    Cluster 0        price range 
1    Cluster 1    going cambridge 
2    Cluster 2   reference number 
3    Cluster 3  reference number 1
4    Cluster 4   going cambridge 1
5    Cluster 5       include free 
6    Cluster 6       british food 
7    Cluster 7        center town 
8    Cluster 8           car type 
9    Cluster 9         need place 
10  Cluster 10  reference number 2
  • Related