How to remove rows with duplicates in pandas dataframe?

Joe

Having a dataframe which contains duplicate values in two columns (A and B):

A B
1 2
2 3
4 5
7 6
5 8

I want to remove duplicates so that only unique values remain:

A B
1 2
4 5
7 6

This command does not provide what I want:

df.drop_duplicates(subset=['A','B'], keep='first')

Any idea how to do this?

jezrael

You can use stack with unstack:

print (df.stack().drop_duplicates().unstack().dropna().astype(int))
   A  B
0  1  2
2  4  5
3  7  6

Solution with boolean indexing:

print (df[~df.stack().duplicated().unstack().any(1)])
   A  B
0  1  2
2  4  5
3  7  6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove duplicates in pandas?

From Dev

How to remove duplicates in pandas?

From Dev

How to drop duplicates from a subset of rows in a pandas dataframe?

From Dev

Python/Pandas - remove rows based on conditions below in a dataframe (similar to remove duplicates but not the same)

From Dev

How to Conditionally Remove Duplicates from Pandas DataFrame with a List

From Dev

How to remove duplicates from a dataframe?

From Dev

Combine rows to remove duplicates in CSV Python and Pandas

From Dev

remove specific rows in dataframe with pandas

From Dev

How to find duplicates in pandas dataframe

From Dev

Pandas: How to remove rows from a dataframe based on a list?

From Dev

Pandas dataframe: group by columnn and let duplicates of this columnn span several rows

From Java

Select rows of pandas dataframe based on column values with duplicates

From Dev

Pandas.Dataframe.duplicated() includes missing rows as duplicates

From Java

Remove rows with duplicate indices (Pandas DataFrame and TimeSeries)

From Dev

Pandas: remove rows of dataframe with unique index value

From Dev

Remove cancelling rows from Pandas Dataframe

From Dev

Pandas: remove rows of dataframe with unique index value

From Dev

How to drop duplicates in Pandas DataFrame by checking for a condition?

From Dev

How to keep first two duplicates in a pandas dataframe?

From Dev

Remove duplicates in dataframe pandas based on values of two columns

From Dev

Remove duplicates in pandas. copy() and drop_duplicates() is removing rows that appear only once

From Dev

Remove Quasi Duplicates In Pandas

From Dev

Excel 2016: how to remove rows that do not have duplicates in column?

From Dev

How to split dataframe or reorder dataframe by rows in pandas

From Dev

Pandas: How can I remove duplicate rows from DataFrame and calculate their frequency?

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns?

Related Related

  1. 1

    How to remove duplicates in pandas?

  2. 2

    How to remove duplicates in pandas?

  3. 3

    How to drop duplicates from a subset of rows in a pandas dataframe?

  4. 4

    Python/Pandas - remove rows based on conditions below in a dataframe (similar to remove duplicates but not the same)

  5. 5

    How to Conditionally Remove Duplicates from Pandas DataFrame with a List

  6. 6

    How to remove duplicates from a dataframe?

  7. 7

    Combine rows to remove duplicates in CSV Python and Pandas

  8. 8

    remove specific rows in dataframe with pandas

  9. 9

    How to find duplicates in pandas dataframe

  10. 10

    Pandas: How to remove rows from a dataframe based on a list?

  11. 11

    Pandas dataframe: group by columnn and let duplicates of this columnn span several rows

  12. 12

    Select rows of pandas dataframe based on column values with duplicates

  13. 13

    Pandas.Dataframe.duplicated() includes missing rows as duplicates

  14. 14

    Remove rows with duplicate indices (Pandas DataFrame and TimeSeries)

  15. 15

    Pandas: remove rows of dataframe with unique index value

  16. 16

    Remove cancelling rows from Pandas Dataframe

  17. 17

    Pandas: remove rows of dataframe with unique index value

  18. 18

    How to drop duplicates in Pandas DataFrame by checking for a condition?

  19. 19

    How to keep first two duplicates in a pandas dataframe?

  20. 20

    Remove duplicates in dataframe pandas based on values of two columns

  21. 21

    Remove duplicates in pandas. copy() and drop_duplicates() is removing rows that appear only once

  22. 22

    Remove Quasi Duplicates In Pandas

  23. 23

    Excel 2016: how to remove rows that do not have duplicates in column?

  24. 24

    How to split dataframe or reorder dataframe by rows in pandas

  25. 25

    Pandas: How can I remove duplicate rows from DataFrame and calculate their frequency?

  26. 26

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  27. 27

    How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

  28. 28

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  29. 29

    How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns?

HotTag

Archive