Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python finding duplicates more than 2 occurrences in all columns

I have a dataframe with 2 columns

id  Date
1   2022-01-01
1   2022-01-01
1   2022-01-01
1   2022-01-02
1   2022-01-02
1   2022-01-02
2   2022-01-01
2   2022-01-01
3   2022-01-01
3   2022-01-01
3   2022-01-01
3   2022-01-01

I need to find the duplicated rows (or duplicated IDs) when there are same values in all columns and those rows should appear more than twice.

The result should be like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

id  Date
1   2022-01-01
1   2022-01-02
3   2022-01-01

>Solution :

Here’s a way to find rows duplicated more than twice:

df2 = df.assign(x=0).groupby(list(df.columns)).count().query('x>2').drop(columns='x').reset_index()

Ouput:

   id        Date
0   1  2022-01-01
1   1  2022-01-02
2   3  2022-01-01

Explanation:

  • add a temporary column x
  • use groupby().count() to obtain unique combinations of the id, Date key with the number of occurrences per group in column x
  • use query() to filter for rows that occur more than twice using the condition x>2
  • drop the temporary column x and use reset_index() to restore id and Date as columns.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading