Q:

How to Retain Columns of lists in a dataframe with a specific value?

Hey I have a dataframe as shown
id     A      B
1      2     ['a', 'c', 'd']
3      4     ['s', 'z', 'a', 'e']
5      6     ['b', 'z', 'd']
7      8     ['a', 'g']

Now, I would like to extract all rows that have 'a' in column "B" 
Desired Output:
id     A      B
1      2     ['a', 'c', 'd']
3      4     ['s', 'z', 'a', 'e']
7      8     ['a', 'g']

Help regarding accomplishing the above in python using Pandas will be appreciated :) 
Thank you in advance for the help :)

A:

Use Series.apply to performance a boolean indexing:
new_df=df[df['B'].apply(lambda x: 'a' in x)]
print(new_df)

   id  A             B
0   1  2     [a, c, d]
1   3  4  [s, z, a, e]
3   7  8        [a, g]

Detail:
df['B'].apply(lambda x: 'a'  in x)
0     True
1     True
2    False
3     True
Name: B, dtype: bool

Also you can use callable:
df.loc[lambda x: x.B.str.join(',').str.contains('a')]

Time Measure for 400 rows
%%timeit
df[pd.DataFrame(df.B.tolist()).eq('a').any(1).values]
3.72 ms ± 105 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
df.loc[lambda x: x.B.str.join(',').str.contains('a')]
1.33 ms ± 90.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%%timeit
df[df['B'].apply(lambda x: 'a' in x)]
786 µs ± 9.62 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

