Python - "case insensitive" in a string or "case ignore"

Samsonite Manly picture Samsonite Manly · Jun 25, 2018 · Viewed 10.7k times · Source

I have a very simple problem. This is for a pandas dataframe ("df"). The answers are all more complex regarding string compare, which I have no use for. Here is the code that works for lowercase and returns only "apple":

df2 = df1['company_name'].str.contains(("apple"), na=False)

I need this to find "apple", "APPLE", "Apple", etc. Something like:

df2 = df1['company_name'].str.contains.caseignore((("apple"), na=False))

is there such a function anywhere?

Thanks.

Answer

Bill the Lizard picture Bill the Lizard · Jun 25, 2018

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)