簡體   English   中英

Pandas DataFrame:在兩個特定的列中獲取具有相同值對的行

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

嗨,我有一個如下數據框

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

謝謝。

這似乎是您想要的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM