0

I have a Pandas dataframe where one of the columns stores lists of string literals. For example, my data looks like the following.

field1, field2
x1, ['tag1', 'tag2']
x2, ['tag1', 'tag3']

I want to get only the records in this dataframe whose field2 list contains tag1 or tag2. So, in the example above, only the record where field1=x1 should be returned.

How can I do this filtering on a Pandas dataframe?

1
  • 1
    Do you mean tag1 and tag2? Commented May 31, 2018 at 3:00

1 Answer 1

1

You need to check for tag2 in the list of field2. Using .apply(), you can achieve this.

df[df.apply(lambda x: 'tag2' in x['field2'],axis=1)]

Output:

    field1  field2
0   x1      [tag1, tag2]
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.