0

I have a Pandas DataFrame that stores lists in one of its columns:

>>> import pandas as pd
>>> d = [{'name': 'john', 'properties': ['a','b']},
...      {'name': 'mary', 'properties': ['a','c']}]
>>> df = pd.DataFrame(d)
>>> df
   name properties
0  john     [a, b]
1  mary     [a, c]

How can I filter by list membership? For example, list all rows that have a 'c' in their properties column.

I know I could explode the properties column:

df.explode('properties')

but I'd prefer to keep it as a list.

1 Answer 1

1

You can use map.

df[df.properties.map(lambda x: 'c' in x)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.