简体   繁体   中英

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

For the dataframe below, how can I return first and third row, as they have identical values in column "c" and "d", and have values opposite of each other in "a" and 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

In other words, I want something similar to:

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

The difference is instead of identical values in 'a' and 'b', the criteria is the values have opposite signs. And I want to return all matching rows.

Thank you very much!

This idea should work:

  1. Self join DF on c,d
  2. Apply condition of opposite values...

A quick and dirty code

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)]

Please let me know if this works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM