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

Popular posts from this blog

php - Autoloader issue not returning Class -

java - How to put two numbers separated by a space into two different arrays -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -