Petr
Petr

Reputation: 1817

Pandas query based on the list of columns and values

Is there a way to automate the filtering process in pandas based on the columns and list of associated values the columns are supposed to take?

Please, see the example here:

import pandas as pd
import seaborn as sns

df = sns.load_dataset('tips')

columns = ['sex']
values = ['Female']

df.query(@columns in @values)

columns2 = ['sex', 'smoker']
values2 = ['Male', 'Yes']
df.query(@columns2 in @values2)

Of course, this does not work but is there a way to do so?

The point is that the solution should be generalized as the length of the lists may differ.

Upvotes: 1

Views: 996

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31166

  • you need to conform with requirements of query
  • this is straight forward with a list comprehension and f-strings
import pandas as pd
import seaborn as sns

df = sns.load_dataset('tips')

columns = ['sex']
values = ['Female']

df.query(" and ".join([f"{c}=='{values[i]}'" for i,c in enumerate(columns)]))

columns2 = ['sex', 'smoker']
values2 = ['Male', 'Yes']
df.query(" and ".join([f"{c}=='{values2[i]}'" for i,c in enumerate(columns2)]))

Upvotes: 3

Related Questions