我的问题与此类似: How to check whether the content of Column A is contained in Column B using Python DataFrame?
不幸的是,在我的案例中,选择的答案导致了非类型错误。
我有一个以下格式的 pandas 数据框:
id,text_1,text_2_compare
1,yyy,yy
2,yxy,xx
3,zzy,zy
4,zzy,x
5,xyx,yx
我想比较列以查看“text_1”中是否包含“text_2_compare”并创建一个新指标。
id,text_1,text_2_compare,match
1,yyy,yy,1
2,yxy,xx,0
3,zzy,zy,1
4,zzy,x,0
5,xyx,yx,1
任何提示或技巧(特别是矢量化实现)将不胜感激!
最佳答案
基于@Onyambu 的回答。
in
可以用来代替 re.findall()
df["match"] = df.apply(lambda v: int(v[2] in v[1]),axis=1)
print(df["match"]
输出:
0 1
1 0
2 1
3 0
4 1
关于python - 按行比较列以进行部分字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391250/