Categories
Python Answers

How to drop consecutive duplicates with Python Pandas?

Spread the love

To drop consecutive duplicates with Python Pandas, we can use shift.

For instance, we write

a.loc[a.shift(-1) != a]

to check if the last column isn’t equal the current one with a.shift(-1) != a.

And then we put a.shift(-1) != a in loc to return a data frame without the consecutive duplicate values.

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 *