列包含第1列 [英] Column contains column 1
本文介绍了列包含第1列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框.我可以测试(C)在每一行上(B)列中的数字是否在字符串列(A)中.
I have a dataframe. I can test whether, (C), on each row, the number in column (B) is in the string column (A).
df = pd.DataFrame({'A': ["me 1 23", "me", "123", "me 12", "12 me"],
'B': [123, 123, 123, 12, 12 ]})
df = df.dropna()
df['C']=df.A.str.contains(r'\b(?:{})\b'.format('|'.join(df.B.astype(str)))).astype(int)
print(df)
这给出了正确的答案:
A B C
0 me 1 23 123 0
1 me 123 0
2 123 123 1
3 me 12 12 1
4 12 me 12 1
但是当我更改第1行的数字(B)时,我得到了第0行的错误答案(C):
But when I change the number (B) on row 1 I get the incorrect answer (C) on row 0:
A B C
0 me 1 23 123 1
1 me 23 0
2 123 123 1
3 me 12 12 1
4 12 me 12 1
推荐答案
我觉得这是逐行检查
[str(y) in x for x , y in zip(df.A,df.B)]
Out[1308]: [False, False, True, True, True]
这篇关于列包含第1列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文