我认为最好是避免循环,因为速度慢。因此,对于按数组检查列,请使用
Series.isin
:
mask1 = c[0].isin(b)
print (mask1 )
0 True
1 False
2 True
3 False
Name: 0, dtype: bool
d1 = np.where(mask1, 'Great', 'sad')
print (d1)
['Great' 'sad' 'Great' 'sad']
对于检查所有值,请使用
DataFrame.isin
具有
any
检查是否至少有一个
True
中的每行
boolean DataFrame
:
mask2 = c.isin(b).any(axis=1)
print (mask2)
0 True
1 True
2 True
3 True
dtype: bool
e1 = np.where(mask2, 'hola', 'False')
print (e1)
['hola' 'hola' 'hola' 'hola']
详细信息:
print (c.isin(b))
0 1 2
0 True True True
1 False False True
2 True False True
3 False False True
如果需要检查
b
在里面
c
使用
numpy.in1d
数据帧的平展值为
1d array
通过
numpy.ravel
:
mask3 = np.in1d(b, c.values.ravel())
print (mask3)
[ True False True True]