如何在 Pandas DataFrame 中查找具有相同值且某些列中带有 same/opposite 符号的匹配行?

How to find matching rows in Pandas DataFrame with identical values with same/opposite signs in certain columns?

对于下面的数据框,我如何 return 第一行和第三行,因为它们在 "c" 和 "d" 列中具有相同的值,并且在 "d" 中具有彼此相反的值"a" 和 b"?

df1=pd.DataFrame([ [1,2,3,4],[5,6,7,8], [-1,-2,3,4]], columns=['a', 'b', 'c', 'd'])

   a  b  c  d
0  1  2  3  4
1  5  6  7  8
2 -1 -2  3  4

换句话说,我想要类似于:

df1.duplicated(subset=['a', 'b', 'c', 'd'])

不同之处在于 'a' 和 'b' 中的值不是相同的,标准是值具有相反的符号。我想 return 所有匹配的行。

非常感谢!

这个想法应该可行:

  1. 在 c,d 上自行加入 DF
  2. 应用相反值的条件...

快速而肮脏的代码

ndf = merge(left=df1,right=df1,on=('c','d'),how='inner')
out = ndf[(ndf.a_x == (-1)*ndf.a_y) & (ndf.b_x == (-1)*ndf.b_y)]

如果可行请告诉我