[英]Python Pandas: Check if string in one column is contained in string of another column in the same row
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
谢谢
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
I could not get either answer @jezreal provided to handle None's in the first column.我无法得到@jezreal 提供的任何一个答案来处理第一列中的 None 。 A slight alteration to the list comprehension is able to handle it:
对列表理解稍加改动就可以处理它:
[x[0] in x[1] if x[0] is not None else False for x in zip(df['A'], df['B'])]
如果您将字符串与字符串进行比较并获得类型错误,您可以这样编码:
df['C'] = df.apply(lambda x: str(x.A) in str(x.B), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.