How to filter a DataFrame column of lists for those that contain a certain item

You can use apply, like this.

In [13]: df[df['col'].apply(lambda x: 'b' in x)]
Out[13]: 
         col
0     [a, b]
2  [a, b, c]

Although generally, storing lists in a DataFrame is a bit awkward – you might find some different representation (columns for each element in the list, MultiIndex, etc) that is easier to work with.

Leave a Comment