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

Keep unique values with only 1 instance

I have the following dataset:

    Col_A   Amounts
0   A      100
1   B      200
2   C      500
3   D      100
4   E      500
5   F      300

The output I am trying to achieve is to basically remove all values based on the "Amounts" column which have a duplicate value and to keep only the rows where there is one unique instance of a value.

Desired Output:

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

    Col_A   Amounts
1   B      200
5   F      300

I have tried to use the following with no luck:

df_1.drop_duplicates(subset=['Amounts'])

This removes the duplicates, however, it still keeps the values which have occurred more than once.

Using the pandas .unique function also provides a similiar undesired output.

>Solution :

You are close, need keep=False for remove all duplicates per Amounts column:

print (df.drop_duplicates(subset=['Amounts'], keep=False))
  Col_A  Amounts
1     B      200
5     F      300
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