1

I'm trying to compare two data frames using an if statement, with the output being a new data frame. I'd like to compare data frame A to B and for each element in A that is larger than the corresponding element in B, return 1 else 0.

A = 5  3  2
    4  7  1
    1  9  5

B = 1  2  9
    2  5  6
    7  2  3

Return:

C = 1  1  0
    1  1  0
    0  1  1

3 Answers 3

1

You can use astype:

#create mask
print (A > B)

       0     1      2
0   True  True  False
1   True  True  False
2  False  True   True

print (A > B).astype(int)

   0  1  2
0  1  1  0
1  1  1  0
2  0  1  1

Next solution is use gt, but it is same:

print (A.gt(B)).astype(int)

   0  1  2
0  1  1  0
1  1  1  0
2  0  1  1

In [13]: %timeit (A > B).astype(int)
The slowest run took 4.71 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 908 µs per loop

In [14]: %timeit (A.gt(B)).astype(int)
The slowest run took 5.16 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 901 µs per loop
Sign up to request clarification or add additional context in comments.

Comments

1

You can compare directly which will generate a boolean mask and then cast the dtype to int using astype:

In [36]:
(A > B).astype(int)

Out[36]:
   0  1  2
0  1  1  0
1  1  1  0
2  0  1  1

The boolean mask looks like:

In [37]:
A > B

Out[37]:
       0     1      2
0   True  True  False
1   True  True  False
2  False  True   True

1 Comment

That was easy enough. Thanks.
1

You can also just multiply Boolean True/False by one to get ones and zeros.

>>> (A > B) * 1
   a  b  c
0  1  1  0
1  1  1  0
2  0  1  1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.