John Taylor
John Taylor

Reputation: 737

comparing pandas list in column with external list

Given a sample dataframe column:

colA           colB
1              [20,40,50,60]
2              [20,70,80]
3              [10,90,100]

and given this list test_lst = [20, 45, 35]

I am trying to develop a way to filter my dataframe to only show rows in which at least one value in the list in colB is in the test_lst

Tried several techniques. Latest was this one:

df[any(x in df['colB'] for x in test_lst)]

I am sure some Pandas/Python experts on here have this one at the ready. I appreciate your time checking out this enigma (at least an enigma to me).

Upvotes: 1

Views: 786

Answers (2)

sitting_duck
sitting_duck

Reputation: 3720

Or using isin():

df[df['colB'].apply(lambda x: pd.Series(x).isin(test_lst).sum()>0)]

   colA              colB
0     1  [20, 40, 50, 60]
1     2      [20, 70, 80]

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195448

You can use set.intersection:

test_lst = [20, 45, 35]
print(df[df.colB.apply(lambda x: set(x).intersection(test_lst)).astype(bool)])

Prints:

   colA              colB
0     1  [20, 40, 50, 60]
1     2      [20, 70, 80]

Upvotes: 6

Related Questions