2017-06-19 26 views
1

您好我有一個像下面熊貓數據框中:獲得具有相同值對排在兩個特定的列

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 

數據幀和我想得行,其中一對值的列「Dist_1」相匹配,並且「Dist_2」。如下所示:

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 

謝謝。

回答

1

這似乎是你想要什麼:

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 

如果排序事項:

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 
相關問題