user2958481
user2958481

Reputation: 607

Pandas DataFrame: get rows with same pair of values in two specific columns

Hi I have a data frame like below

id  other_things  Dist_1  Dist_2
1   a             20.3    16.4
2   b             15.4    480.2
3   a             12.6    480.2
4   c             20.3    16.4
5   d             12.6    480.2
6   e             52.5    584.5

And I want to get rows where the pair of values matches in columns "Dist_1" and "Dist_2". like follows,

id  other_things  Dist_1  Dist_2
1   a             20.3    16.4
4   c             20.3    16.4
3   a             12.6    480.2
5   d             12.6    480.2

Thank you.

Upvotes: 3

Views: 2061

Answers (1)

gold_cy
gold_cy

Reputation: 14216

This seems like what you want:

df[df.duplicated(['Dist_1','Dist_2'], keep=False)]

   id other_things  Dist_1  Dist_2
0   1            a    20.3    16.4
2   3            a    12.6   480.2
3   4            c    20.3    16.4
4   5            d    12.6   480.2

If sorting matters:

df[df.duplicated(['Dist_1','Dist_2'], keep=False)].sort_values('Dist_2')

   id other_things  Dist_1  Dist_2
0   1            a    20.3    16.4
3   4            c    20.3    16.4
2   3            a    12.6   480.2
4   5            d    12.6   480.2

Upvotes: 4

Related Questions