Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 581
Samsonite Manly Avatar asked Jun 25 '18 15:06

Samsonite Manly


People also ask

How do you ignore a case in a string in Python?

Approach No 1: Python String lower() Method This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.

Does string contain case-sensitive Python?

Python strings are case sensitive, so these equality check methods are also case sensitive.

How do I ignore case in STR contain?

str. contains has a case parameter that is True by default. Set it to False to do a case insensitive match.

How does ignore case compare to string?

The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.


1 Answers

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) 
like image 64
Bill the Lizard Avatar answered Sep 16 '22 21:09

Bill the Lizard