1

I have the following simple data frame:

stores = ['a', 'a', 'b', 'b', 'b']
brands = ['Nike', 'Nike', 'Adidas', 'Nike', 'Adidas']
colours = ['Black', 'Black', 'White', 'Black', 'Black']
data = dict(stores=stores, brands=brands, colours=colours)
df = pd.DataFrame(data, columns=data.keys())

I'd like to query this using a list of columns and a corresponding list of values. For e.g.

columns = ['stores', 'brands']
values = ['a', 'Nike']
df[columns == values]

Is this possible?

2 Answers 2

2

This should be possible using numpy.logical_and with reduce for an arbitrary number of conditions:

import numpy as np

df[np.logical_and.reduce([df[col] == val for col, val in zip(columns, values)])]

Results:

  stores brands colours
0      a   Nike   Black
1      a   Nike   Black
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this in a similar way to thesilkworm's answer using only pandas:

query = " & ".join([c + " == '" + v + "'" for c,v in zip(columns, values)])
df.query(query)

Output using the above code:

>>> query = " & ".join([c + " == '" + v + "'" for c,v in zip(columns, values)])
>>> query
"stores == 'a' & brands == 'Nike'"
>>> df.query(query)
  stores brands colours
0      a   Nike   Black
1      a   Nike   Black

Note the inclusion of single quotes around v in the list comprehension. These are important, since we're comparing a string value. For more info, see the query documentation for pandas.

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.