Skip to content
Advertisement

Pandas – Compare each row with one another across dataframe and list the amount of duplicate values

I would like to add a column to an existing dataframe that compares every row in the dataframe against each other and list the amount of duplicate values. (I don’t want to remove any of the rows, even if they are entirely duplicated with another row)

The duplicates column should show something like this:

JavaScript

Advertisement

Answer

IIUC, you can convert your dataframe to an array of sets, then use numpy broadcasting to compare each combination (except the diagonal) and get the max intersection:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement