How to find rows of list items with a key-search element in pandas?

I have a pandas dataframe with lists. I want to be able to search using one item in the list. For example,

import pandas as pd
# initialize list elements
data = [[10, ["a", "b", "c"] ], [20, ["d", "e", "f"]], [30, ["c"]]] 

# Create the pandas DataFrame with column name is provided explicitly
df = pd.DataFrame(data, columns=['Numbers', "Characters"])
# print data

Numbers     Characters_List
0   10  [a, b, c]
1   20  [d, e, f]
2   30  ["c"]

If I search for "e", the output should be,

Numbers     Characters_List
0   20  [d, e, f]

Thanks in advance!

>Solution :

df[df.apply(lambda x: "e" in x.Characters, axis=1)]

Leave a Reply