Series.str.contains has a case parameter that is True by default. Set it to False to do a case insensitive match.
df2 = df1['company_name'].str.contains("apple", na=False, case=False)
Answer from Bill the Lizard on Stack OverflowSeries.str.contains has a case parameter that is True by default. Set it to False to do a case insensitive match.
df2 = df1['company_name'].str.contains("apple", na=False, case=False)
If you want to do the exact match and with strip the search on both sides with case ignore,
df[df['Asset Name'].str.strip().str.match('searchstring'.strip(), case=False)]
python - Using Pandas str.contains using Case insensitive - Stack Overflow
python - Filtering pandas dataframe rows by contains str - Stack Overflow
How do I remove the case sensitivity while doing query
regex - pandas python: access row by case insensitive label - Stack Overflow
You could either use .str again to get access to the string methods, or (better, IMHO) use case=False to guarantee case insensitivity:
>>> df = pd.DataFrame({"body": ["ball", "red BALL", "round sphere"]})
>>> df[df["body"].str.contains("ball")]
body
0 ball
>>> df[df["body"].str.lower().str.contains("ball")]
body
0 ball
1 red BALL
>>> df[df["body"].str.contains("ball", case=False)]
body
0 ball
1 red BALL
>>> df[df["body"].str.contains("ball", case=True)]
body
0 ball
(Note that if you're going to be doing assignments, it's a better habit to use df.loc, to avoid the dreaded SettingWithCopyWarning, but if we're just selecting here it doesn't matter.)
(Note #2: guess I really didn't need to specify 'round' there..)
You can also use contains inside query:
In [2]: df = pd.DataFrame({'body': ['Ball', 'cUbE', 'bAll'], 'color': ['red', 'green', 'blue']})
In [3]: df
Out[3]:
body color
0 Ball red
1 cUbE green
2 bAll blue
In [4]: df.query('body.str.contains("ball", case=False).values')
Out[4]:
body color
0 Ball red
2 bAll blue
If you try to match multiple patterns use |:
In [5]: df.query('body.str.contains("ball|cube", case=False).values')
Out[5]:
body color
0 Ball red
1 cUbE green
2 bAll blue
I'm trying to do a quick database search in python using pandas.
data = pd.read_excel(open('Stockroom_Inventory_May_4_2020.xlsx', 'rb'),sheet_name='Chemical')
#This works fine for case insensitive search
data.loc[data['Item Name *'].str.contains('Ferro', case = False)][['Item Name *','Location','Sub-location','Location Details']]
#Can't use case = False with startswith
data.loc[data['Item Name *'].str.startswith('Ferro')][['Item Name *','Location','Sub-location','Location Details']]
Is there a way to get around this, even making everything from the excel file lowercase would be fine.
Thanks for your help