Issue
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
df
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]
Solution
df[df.apply(lambda x: "e" in x.Characters, axis=1)]
Answered By - MoRe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.