简体   繁体   中英

Using Pandas str.contains using Case insensitive

My Dataframe:

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

I am trying to create a new df having data related to apple only.

My code:

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

It is not helping to get the correct output. Also, I don't want to change the complete col1 tolower()

It does work for me the only problem I found was that you are selecting col1 instead of Col1 . The following code:

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)

prints

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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