Sid
Sid

Reputation: 552

Check if one or more elements of a list are present in Pandas column

This question is an extension of the following question Check if pandas column contains all elements from a list

In the question, for deriving output, all the members of a list are checked for a list in a Pandas column. My need is to check one or more elements of the list, i.e even if only one element of the list matches in the pandas column, I want to consider that in the output

The sample data would be

frame = pd.DataFrame({'a' : ['a,b,c', 'a,c,f', 'b,d,f','a,z,c']})


letters = ['a','c','m']

I want to get all the rows from the df where one or more elements of the letters list are found

Upvotes: 0

Views: 279

Answers (1)

jezrael
jezrael

Reputation: 863791

You can change issubset to isdisjoint for found not common values, so then is added ~ for inverse mask:

letters = ['a','c','m']

letters_s = set(letters)
df = frame[~frame.a.str.split(',').map(letters_s.isdisjoint)]
print(df)
       a
0  a,b,c
1  a,c,f
3  a,z,c

Or first solution is possible modify by np.any for test at least one match:

contains = [frame['a'].str.contains(i) for i in letters]
df = frame[np.any(contains, axis=0)]

Upvotes: 3

Related Questions