37

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.

Samsonite Manly
  • 569
  • 2
  • 7
  • 15
  • 2
    What is "df" ? A panda dataframe ? If yes please edit your post to make this clear and add the "panda" tag, else explain what is this "df" thing. – bruno desthuilliers Jun 25 '18 at 15:17
  • See this question https://stackoverflow.com/questions/6579876/how-to-match-a-substring-in-a-string-ignoring-case – nalyd88 Jun 25 '18 at 15:19
  • 1
    I'll do something like this: `df2 = df1['company_name'].str.upper().contains("ApPlE".upper())` and check everythoing uppercase. – DDS Jun 25 '18 at 15:51

2 Answers2

83

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)
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

If you want to do the exact match and with strip the search on both sides with case ignore,

pd[pd['Asset Name'].str.strip().str.match('searchstring'.strip(),case=False)]
mnille
  • 1,328
  • 4
  • 16
  • 20