python - How to subset pandas DataFrame whose column values are lists? -
i have pandas dataframe df looks this:
a b 0 ['a','b'] 1 ['c','d'] 2 ['a','c'] 3 ['b','d'] 4 ['a','d'] now, wish subset df selecting rows in 'a' belongs list in b, desired output being:
a b 0 ['a','b'] 2 ['a','c'] 4 ['a','d'] naively, tried df['a' in df['b']], doesn't seem work. how go doing this?
using apply 1 way filter.
in [39]: df[df['b'].apply(lambda x: 'a' in x)] out[39]: b 0 0 [a, b] 2 2 [a, c] 4 4 [a, d]
Comments
Post a Comment