繁体   English   中英

使用 Pandas str.contains 使用不区分大小写

[英]Using Pandas str.contains using Case insensitive

我的 Dataframe:

Req Col1
1   Apple is a fruit
2   Sam is having an apple
3   Orange is orange

我正在尝试创建一个仅包含与苹果相关的数据的新 df。

我的代码:

Apple=df1[df1.col1.str.contains('apple',case=False)]

获得正确的 output 无济于事。 另外,我不想更改完整的 col1 tolower()

它对我有用,我发现的唯一问题是您选择col1而不是Col1 以下代码:

df1 = pd.DataFrame({'Req': [1,2,3],
                    'Col1': ['Apple is a fruit',
                             'Sam is having an apple',
                             'Orange is orange']})
Apple=df1[df1.Col1.str.contains('apple',case=False)]
print(Apple)

印刷

>>>    Req                    Col1
>>> 0    1        Apple is a fruit
>>> 1    2  Sam is having an apple

暂无
暂无

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

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