Pandas DataFrame:在两个特定列中获取具有相同值对的行

时间:2017-06-19 00:54:21

标签: python pandas dataframe group-by

嗨,我有一个如下数据框

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

我希望在列#34; 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 个答案:

答案 0 :(得分:3)

这看起来像你想要的:

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