比较pandas中两个数据框内的元素

时间:2016-01-04 16:34:29

标签: python pandas

我尝试使用if语句比较两个数据帧,输出是一个新的数据帧。我想比较数据框A到B,并且对于A中每个元素大于B中的对应元素,返回1,否则为0。

A = 5  3  2
    4  7  1
    1  9  5

B = 1  2  9
    2  5  6
    7  2  3

返回:

C = 1  1  0
    1  1  0
    0  1  1

3 个答案:

答案 0 :(得分:1)

您可以使用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

下一个解决方案是使用gt,但它是相同的:

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

答案 1 :(得分:1)

你可以直接比较哪个会生成一个布尔掩码,然后使用dtypeastype强制转换为int:

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

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

布尔掩码如下所示:

In [37]:
A > B

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

答案 2 :(得分:1)

您也可以将布尔值True / False乘以1得到1和0。

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