2

I have a pandas dataframe where one column contains list objects which gives me dtype: object. The list objects are of different lengths.

        features           other_features
0        ["foo", "bar"]                2
1        ["foo", "dog"]                1
2        ["dog"]                       4

I want to select the rows in my dataframe where the list has elements that are in a different list external_list = ["dog", "cat"].

In this case I would like the result to be the rows that contains dog or cat together with the other coulmns, which would be:

        features           other_features
1        ["foo", "dog"]                1
2        ["dog"]                       4

I have tried isin, but from what I understand it requires the column type of the element to look at to not be a list of more than one object.

filter = df["features"].isin(["dog", "cat"])

It results in every element being False, which it should not since some should be True, and it is not returning the rest of the columns.

How can I solve this?

2 Answers 2

2

Use map with comparing list converted to sets tested by isdisjoint, ~ is for inverse mask:

#if string repr of list
#import ast
#df['features'] = df['features'].apply(ast.literal_eval)

external_list = ["dog", "cat"]
df = df[~df.features.map(set(external_list).isdisjoint)]
print (df)
     features  other_features
1  [foo, dog]               1
2       [dog]               4
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! At first the resulting data frame was empty, the comments with literal_eval solved it and it gives me exactly what I wanted.
0

Let us do "explode" on axis =0, then we can do isin

m=pd.DataFrame(df.features.tolist(),index=df.index).isin(external_list).any(1)
df=df[~m]

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.