Search code examples
pythonlistuppercaselowercase

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?


Solution

  • 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