简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM