简体   繁体   中英

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!

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

I think you should use the method chain like below. .uppper() as method(parenthesis) and additional .str accessor for the following .contains() method.

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

Example

import pandas as pd

df = pd.DataFrame(['a'])
print(df[df[0].str.upper().str.contains('A')])

   0
0  a

Option 1

Convert to upper case using df.apply(str.upper)

In [1283]: my_df = pd.DataFrame({'name': ['my_target', 'foo', 'bar', 'My_TarGet']})

In [1279]: my_df[my_df.name.apply(str.upper).str.contains('MY_TARGET')]
Out[1279]: 
        name
0  my_target
3  My_TarGet

For this case, you can specify regex=False for an additional speedup.


Option 2

Use the regex flag re.I (ignore case) with df.str.contains ( import re first)

In [1282]: my_df[my_df.name.str.contains('MY_TARGET', flags=re.I)]
Out[1282]: 
        name
0  my_target
3  My_TarGet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM