Menu Close

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

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'.

Posted in Python, Python Answers