返回列之间具有唯一对的行 [英] return rows with unique pairs across columns
问题描述
我正在尝试查找两列中具有唯一值对的行,因此,此数据框:
I'm trying to find rows that have unique pairs of values across 2 columns, so this dataframe:
A B
1 0
2 0
3 0
0 1
2 1
3 1
0 2
1 2
3 2
0 3
1 3
2 3
如果将
翻转,它们将仅减少到不匹配的行,例如1和3是我只想返回一次的组合.因此,如果将列翻转(3和1),请检查是否存在相同的一对,可以将其删除.我要获取的表是:
will be reduced to only the rows that don't match up if flipped, for instance 1 and 3 is a combination I only want returned once. So a check to see if the same pair exists if the columns are flipped (3 and 1) it can be removed. The table I'm looking to get is:
A B
0 2
0 3
1 0
1 2
1 3
2 3
如果列被翻转,则每对被映射的值对中只有一次出现.
Where there is only one occurrence of each pair of values that are mirrored if the columns are flipped.
推荐答案
我认为您可以使用 drop_duplicates
:
I think you can use apply
sorted
+ drop_duplicates
:
df = df.apply(sorted, axis=1).drop_duplicates()
print (df)
A B
0 0 1
1 0 2
2 0 3
4 1 2
5 1 3
8 2 3
使用 numpy.sort
的快速解决方案:
Faster solution with numpy.sort
:
df = pd.DataFrame(np.sort(df.values, axis=1), index=df.index, columns=df.columns)
.drop_duplicates()
print (df)
A B
0 0 1
1 0 2
2 0 3
4 1 2
5 1 3
8 2 3
无需使用 DataFrame.min
和 DataFrame.max
:
Solution without sorting with DataFrame.min
and DataFrame.max
:
a = df.min(axis=1)
b = df.max(axis=1)
df['A'] = a
df['B'] = b
df = df.drop_duplicates()
print (df)
A B
0 0 1
1 0 2
2 0 3
4 1 2
5 1 3
8 2 3
这篇关于返回列之间具有唯一对的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!