5

I have a dataframe as given below.

import pandas as pd


raw_data = {'score': [1,2,3], 
        'tags': [['apple','pear','guava'],['truck','car','plane'],['cat','dog','mouse']]}


df = pd.DataFrame(raw_data, columns = ['score', 'tags'])

df.query("score==1") gives the first row as result.

But df.query("tags='apple'") gives error.

How to write query for the column 'tags'.

3
  • 1
    You've stored lists in your df, the query method is unable to evaluate your expression to handle this. To filter the df you'd need to do df[df['tags'].apply(lambda x: 'apple' in x)]. Storing non-scalar values in a df is non-performant, you can't expect the usual pandas operations to work like normal Commented Jun 21, 2018 at 10:31
  • @EdChum, Oops, edited my post at the same time. But I'm going to add another alternative too :) Commented Jun 21, 2018 at 10:37
  • @jpp no worries I'm sure there is a dupe for this although not specifically for getting query to handles lists Commented Jun 21, 2018 at 10:54

1 Answer 1

1

You cannot use pd.DataFrame.query to test membership of a string in lists within a series of lists. Holding lists in Pandas dataframes is not recommended as you lose vectorised functionality.

With your existing dataframe, you can instead calculate a mask using pd.Series.apply:

res = df[df['tags'].apply(lambda x: 'apple' in x)]

print(res)

   score                  tags
0      1  [apple, pear, guava]

Or you can use a list comprehension:

res = df[['apple' in x for x in df['tags']]]

A third option is to use set:

res = df[df['tags'].apply(set) >= {'apple'}]

The last option, although expensive, may suit when you are testing for existence of multiple tags. In each case, we are constructing a Boolean series, which we then use to mask the dataframe.

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.