Categories
Python Answers

How to remove pandas rows with duplicate indices with Python?

Spread the love

Sometimes, we want to remove pandas rows with duplicate indices with Python.

In this article, we’ll look at how to remove pandas rows with duplicate indices with Python.

How to remove pandas rows with duplicate indices with Python?

To remove pandas rows with duplicate indices with Python, we can use the index.duplicated method.

For instance, we write

df = df[~df.index.duplicated(keep='first')]

to call df.index.duplicated where df is a Pandas data frame.

We call it with the keep argument set to 'first' and add ~ to keep the first instance of the item and remove the duplicates.

And then we put that in the square brackets to get a data frame without the entries with duplicate indices and assign that to df.

Conclusion

To remove pandas rows with duplicate indices with Python, we can use the index.duplicated method.

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 *