简体   繁体   中英

Check if Series contains any element from a list

I'm reading a large CSV file and one of the columns has below representation.

import pandas as pd

df['col1'] = pd.Series(
    ["37", "AWESOME House", "Yellow Cottage, 107", "14"], dtype='object'
)

My code uses "vectorized string methods" to return desired data in timely fashion.

Simplified code to illustrate some parts of logic.

import numpy as np

sth = np.where(
    <check condition>,
    df['col1'].str.lower(),
    df['some_other_column'].whatever()
)

Next I'd like to check if each value in my Series contains any element from below list.

check_list = ['a', 'b', 'c']

So expected result (for "check condition") would be:

False
True
True
False

I tried this

np.where(
    np.any([x in df['col1'].str.lower() for x in check_list])
...

but received error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How could I solve my problem correctly?

Use Series.str.contains with joined values of list by |for regex or with case=False for case non sensitive search:

print (df['col1'].str.contains('|'.join(check_list), case=False))
0    False
1     True
2     True
3    False
Name: col1, dtype: bool

Without regex :

print (df['col1'].apply(lambda x: any([i in x.lower() for i in check_list])))
0    False
1     True
2     True
3    False
Name: col1, dtype: bool

print ([any([i in x.lower() for i in check_list]) for x in df['col1']])
[False, True, True, False]

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