RPacker
RPacker

Reputation: 183

Python list integrateUpper/Lowercase check?

I currently use following code to select rows from a dataframe:

list = ['3d block','3D Block','3D block','3d Block','cafe','Cafe']
data[data['description'].str.contains('|'.join(list))]

As you can see, I currently implement different queries for upper/lowercase, as I realized that '3d print' didn't really match all the items in question.

Can the code above be improved or shortened, so that the list includes all possible variations of upper/lowercase characters, both for one and two words?

Upvotes: 1

Views: 1714

Answers (2)

Moses Koledoye
Moses Koledoye

Reputation: 78556

You can do a case InSeNsitIvE selection by passing the case parameter as False:

lst = ['3D Block', 'cafe']
data[data['description'].str.contains('|'.join(lst), case=False)]

And remember to not use list as a name as this will shadow the builtin list function, and make it unusable later in your code.


Reference:

pandas.Series.str.contains

Upvotes: 2

R. S. Nikhil Krishna
R. S. Nikhil Krishna

Reputation: 4230

You can simply convert them all to lower using the string.lower() function

Make your list contain only lower case characters. But you can do a safe check for that too

my_list=['3d block', 'cafe']

# As karthik suggested, just to be safe, convert your list to lowercase
for i in range(len(my_list)):
    my_list[i]=ele.lower()

my_query='3D bLoCk'
if my_query.lower() in my_list:
    print('yes')

prints yes

Upvotes: 2

Related Questions