Search code examples
python-3.xpandasuppercase

Pandas: check column values by ignoring cases (convert cases)


I am trying to check values of a pandas column using the following condition:

my_df[my_df.name.str.contains('MY_TARGET')]

This works fine. But since I need to convert the name column to upper case, I did the following but didn't work:

my_df[my_df.name.str.upper.contains('MY_TARGET')]

What's the proper way to perform the column value checks by ignoring cases? Thanks!


Solution

  • You can simply use case = False parameter ie.

    df = pd.DataFrame({'name': ['my_target', 'foo', 'bar', 'My_TarGet']}) #Coldspeed data
    df[df['name'].str.contains('my_target', case=False)]
    

    Output :

           name
    0  my_target
    3  My_TarGet