通过比较两个系列创建一个逻辑熊猫系列

时间:2015-11-11 11:52:18

标签: pandas

在熊猫中,我试图将两个系列组合成一个逻辑的

f = pd.Series(['a','b','c','d','e'])
x = pd.Series(['a','c'])

结果我想要系列

[1, 0, 1, 0, 0]

我试过

f.map(lambda e: e in x)

系列f很大(30000)因此循环遍历元素(使用map)可能效率不高。什么是好方法?

1 个答案:

答案 0 :(得分:2)

使用isin

In [207]:
f = pd.Series(['a','b','c','d','e'])
x = pd.Series(['a','c'])
f.isin(x)

Out[207]:
0     True
1    False
2     True
3    False
4    False
dtype: bool

如果您愿意,可以使用astype转换dtype:

In [208]:
f.isin(x).astype(int)

Out[208]:
0    1
1    0
2    1
3    0
4    0
dtype: int32
相关问题