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

python pandas how to copy duplicate value to new Column and keep the first duplicate value empty?

I have an dataframe like this

  sku
FAT-001
FAT-001
FAT-001
FAT-002
FAT-002

I want to create another column depend on the duplicate sku value. The first duplicate value must me empty in dup-sku column. I want to keep only duplicate sku in my dup-sku column. So my expected dataframe will be look like this:

  sku        dup-sku
FAT-001      #empty 
FAT-001      FAT-001
FAT-001      FAT-001
FAT-002      #emty  
FAT-002      FAT-002
FAT-003      

The first value of duplicate in dup-sku column must be empty

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

>Solution :

Would this work for your example ?

df['dup']=df['sku']
df['dup'].loc[~df['sku'].duplicated(keep='first')]=''
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