python pandas.Series.isin with case insensitive

Viewed 17961

I want to filter out some rows with one of DataFrame's column which data is in a list.

df[df['column'].isin(mylist)]

But I found that it's case sensitive. Is there any method using ".isin()" with case insensitive?

5 Answers

I would put my list into a CSV and load it as a dataframe. Afterwards I would run the command:

df_done = df[df["Server Name"].str.lower().isin(df_compare["Computer Name"].str.lower())]

This avoids using for loop and can handle large amounts of data easily.

df = 5000 rows
df_compare = 1000 rows

In Pandas version 1.1.0, you can use the fullmatch function with your list converted to | conditions. match has an option to turn off case-sensitivity.

Example: Consider the dataframe df of

    Apple   Banana      Orange
0   A       Boy         Cat
1   Ivan    Elephant    Gold

df.Apple.str.lower().isin(['a', 'c', 'd', 'elephant']) returns the following result:

0     True
1    False
Name: Apple, dtype: bool

while df.Banana.str.lower().isin(['a', 'c', 'd', 'elephant']) returns:

0    False
1     True
Name: Banana, dtype: bool

Pandas fullmatch function, with case=False

To achieve the same purpose as above, one can use
df.Apple.str.fullmatch('a|c|d|elephant', case=False)
and
df.Banana.str.fullmatch('a|c|d|elephant', case=False)
, which will give the correct results respectively.

0     True
1    False
Name: Apple, dtype: bool

and

0    False
1     True
Name: Banana, dtype: bool
Related