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
Python Pandas: Check if string in one column is contained in string of another column in the same row
提问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 apply
with in
:
你需要apply
有in
:
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 comprehension
is faster, but there has to be no NaN
s:
另一个解决方案list comprehension
更快,但必须没有NaN
s:
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'])]