简体   繁体   English

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

[英]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.

相关问题 如果一个字符串列包含在 Pandas 的另一列中,则合并两个数据框 - Merge two dataframe if one string column is contained in another column in Pandas Pandas:检查一列中的字符串值是否是 dataframe 同一行中另一列的字符串的一部分 - 当前脚本返回全部是 - Pandas: check if string value in one column is part of string of another column in same row of dataframe - current script returning all Yes 如何检查一个 Pandas 列的字符串值是否包含在另一个 Pandas 列的字符串值中? - How to check whether the string value of a Pandas Column is contained in the string value of another Pandas Column? 检查字符串是否在另一列熊猫中 - Check if string is in another column pandas Python数据框:根据数据框另一列的字符串行中是否包含列名,将行填充为1或0 - Python Dataframe: Fill in Row as 1 or 0 Based on If Column Name is Contained in String Row of Another Column in Dataframe 如何检查PANDAS DataFrame列中是否包含一系列字符串,并将该字符串分配为行中的新列? - How to check if a series of strings is contained in a PANDAS DataFrame columns and assign that string as a new column in the row? Python数据框:如果列名称包含在另一列的字符串行中,则为1,否则为0 - Python Data frame: If Column Name is contained in the String Row of Another Column Then 1 Otherwise 0 Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column Python - 另一个是一个字符串列吗? - Python - Is one string column in another? 检查 pandas 中是否至少有一列包含字符串 - Check if at least one column contains a string in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM