Categories
Python Answers

How to get rows based on distinct values from one column with Python Pandas?

Spread the love

To get rows based on distinct values from one column with Python Pandas, we call the drop_duplicates method.

For instance, we write

df = df.drop_duplicates('COL2')

to call drop_duplicates with 'COL2' to drop the duplicate values from the COL2 column.

We can use the keep argument to keep the first or last values.

For instance, we write

df = df.drop_duplicates('COL2', keep='first')

to keep the first value with keep='first'.

And we use

df = df.drop_duplicates('COL2', keep='last')

to keep the lastvalue with keep='last'.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *