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:
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.