简体   繁体   English

在 Pandas 中选择行,其中一列中的值是另一列中值的子字符串

[英]Select rows in pandas where value in one column is a substring of value in another column

I have a dataframe below我在下面有一个数据框

>df = pd.DataFrame({'A':['apple','orange','grape','pear','banana'], \
                    'B':['She likes apples', 'I hate oranges', 'This is a random sentence',\
                         'This one too', 'Bananas are yellow']})

>print(df)

    A       B
0   apple   She likes apples
1   orange  I hate oranges
2   grape   This is a random sentence
3   pear    This one too
4   banana  Bananas are yellow

I'm trying to fetch all rows where column B contains the value in column A.我正在尝试获取 B 列包含 A 列中的值的所有行。

Expected Result:预期结果:

    A       B
0   apple   She likes apples
1   orange  I hate oranges
4   banana  Bananas are yellow

I'm able to do fetch only one row using我只能使用

>df[df['B'].str.contains(df.iloc[0,0])]

    A       B
0   apple   She likes apples

How can I fetch all such rows?我怎样才能获取所有这些行?

Use DataFrame.apply with convert both values to lower and test contains by in and filter by boolean indexing :使用DataFrame.apply将两个值都转换为较低和测试包含通过in并通过boolean indexing过滤:

df = df[df.apply(lambda x: x.A in x.B.lower(), axis=1)]

Or list comprehension solution:或列表理解解决方案:

df = df[[a in b.lower() for a, b in zip(df.A, df.B)]]

print (df)
        A                   B
0   apple    She likes apples
1  orange      I hate oranges
4  banana  Bananas are yellow

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找将一列的值作为另一列中的子字符串以及熊猫中的其他 OR 条件的行 - Find rows which have one column's value as substring in another column along with other OR conditions in pandas Select pandas 列是 substring 的行 - Select pandas rows where column is a substring Pandas select 行如何根据一列的值然后改变另一列的值 - Pandas how to select rows based on one column value and then change another column's value 将pandas列拆分为多行,其中拆分为另一列的值 - Split pandas column into multiple rows, where splitting is on the value of another column 选择一列中的特定值并从熊猫的另一列之前/之后获取 n 行 - Select specific value in one column and get n rows before/after from another column in pandas Pandas Groupby-如果多行超过另一行的值,则选择一列中值最高的行 - Pandas Groupby - select row with highest value in one column if multiple rows exceed value in another 删除一列中的值等于另一列中的值的行 - Remove rows where value in one column equals value in another Pandas - 检查列中的值是否是同一列中另一个值的 substring - Pandas - Check if a value in a column is a substring of another value in the same column 删除日期与pandas中另一列的最大值对齐的行 - dropping rows where date aligns with max value of another column in pandas Python pandas dataframe:删除列中的值存在于另一个中的行 - Python pandas dataframe: delete rows where value in column exists in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM