Python Pandas:检查一列中的字符串是否包含在同一行中另一列的字符串中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43855685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:33:40  来源:igfitidea点击:

Python Pandas: Check if string in one column is contained in string of another column in the same row

pythonpandas

提问by swang16

I have a dataframe like this:

我有一个这样的数据框:

RecID| A  |B
----------------
1    |a   | abc 
2    |b   | cba 
3    |c   | bca
4    |d   | bac 
5    |e   | abc 

And want to create another column, C, out of A and B such that for the same row, if the string in column A is contained in the string of column B, then C = True and if not then C = False.

并且想要从 A 和 B 中创建另一列 C,这样对于同一行,如果 A 列中的字符串包含在 B 列的字符串中,则 C = True,否则 C = False。

The example output I am looking for is this:

我正在寻找的示例输出是这样的:

RecID| A  |B    |C 
--------------------
1    |a   | abc |True
2    |b   | cba |True
3    |c   | bca |True
4    |d   | bac |False
5    |e   | abc |False

Is there a way to do this in pandas quickly and without using a loop? Thanks

有没有办法在不使用循环的情况下快速地在Pandas中做到这一点?谢谢

回答by jezrael

You need applywith in:

你需要applyin

df['C'] = df.apply(lambda x: x.A in x.B, axis=1)
print (df)
   RecID  A    B      C
0      1  a  abc   True
1      2  b  cba   True
2      3  c  bca   True
3      4  d  bac  False
4      5  e  abc  False

Another solution with list comprehensionis faster, but there has to be no NaNs:

另一个解决方案list comprehension更快,但必须没有NaNs:

df['C'] = [x[0] in x[1] for x in zip(df['A'], df['B'])]
print (df)
   RecID  A    B      C
0      1  a  abc   True
1      2  b  cba   True
2      3  c  bca   True
3      4  d  bac  False
4      5  e  abc  False

回答by Doubledown

I could not get either answer @jezreal provided to handle None's in the first column. A slight alteration to the list comprehension is able to handle it:

我无法得到@jezreal 提供的任何一个答案来处理第一列中的 None 。对列表理解稍加改动就可以处理它:

[x[0] in x[1] if x[0] is not None else False for x in zip(df['A'], df['B'])]