Reputation: 1817
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
Reputation: 31166
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