Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename equal rows in same column

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading