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

3

你好,我有一个数据框如下:

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个回答

4
这似乎是您想要的内容:

这看起来像是您想要的:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接