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

Remove duplicates in a row pandas

I have a df

Name  Symbol              Dummy
A     (BO),(BO),(AD),(TR)   2
B     (TV),(TV),(TV)        2
C     (HY)                  2
D     (UI)                  2

I need df as

Name  Symbol              Dummy
A     (BO),(AD),(TR)        2
B     (TV)                  2
C     (HY)                  2
D     (UI)                  2

Tried with this function but not working as expected.

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

drop_duplicates

>Solution :

Split the strings around delimiter ,, then dedupe using dict.fromkeys which also preserves the order of strings, finally join around delimiter ,

df['Symbol'] = df['Symbol'].str.split(',').map(dict.fromkeys).str.join(',')

  Name          Symbol  Dummy
0    A  (BO),(AD),(TR)      2
1    B            (TV)      2
2    C            (HY)      2
3    D            (UI)      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