繁体   English   中英

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

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

我在下面有一个数据框

>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

我正在尝试获取 B 列包含 A 列中的值的所有行。

预期结果:

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

我只能使用

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

    A       B
0   apple   She likes apples

我怎样才能获取所有这些行?

使用DataFrame.apply将两个值都转换为较低和测试包含通过in并通过boolean indexing过滤:

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

或列表理解解决方案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM