Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
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]

Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading