python pandas.Series.isin with case insensitive
Convert it to a str
using the str
method and get the lowercase version
In [23]: df =pd.DataFrame([['A', 'B', 'C'], ['D', 'E', 6]], columns=['A', 'B', '
...: C'])
In [24]: df
Out[24]:
A B C
0 A B C
1 D E 6
In [25]: df.A
Out[25]:
0 A
1 D
Name: A, dtype: object
In [26]: df.A.str.lower().isin(['a', 'b', 'c'])
Out[26]:
0 True
1 False
Name: A, dtype: bool
One way would be by comparing the lower or upper case of the Series with the same for the list
df[df['column'].str.lower().isin([x.lower() for x in mylist])]
The advantage here is that we are not saving any changes to the original df or the list making the operation more efficient
Consider this dummy df:
Color Val
0 Green 1
1 Green 1
2 Red 2
3 Red 2
4 Blue 3
5 Blue 3
For the list l:
l = ['green', 'BLUE']
You can use isin()
df[df['Color'].str.lower().isin([x.lower() for x in l])]
You get
Color Val
0 Green 1
1 Green 1
4 Blue 3
5 Blue 3
I prefer to use the general .apply
myset = set([s.lower() for s in mylist])
df[df['column'].apply(lambda v: v.lower() in myset)]
A lookup in a set
is faster than a lookup in a list