Suppose I have a dataframe of celebrities with their age, ethnicity, height, industry etc.
I want to create a function where I can filter the dataframe systematically such that multiple filters can be applied.
e.g
def filter_data(df, filter_col, filter_val, filter_amount):
if filter_amount == 1:
df = df[df.filter_col[0] == filter_val[0]]
if filter_amount == 2:
df = df[(df.filter_col[0] == filter_val[0]) & (df.filter_col[1] == filter_val[1])]
etc
Where filter_col is a list of columns for which you wish to filter by, and filter_val is also a list of values and filter_amount is an integer
I want it to be systematical so that for any filter amount it would proceed to filter the dataset based on the values of the list without having to manually code it out
help.
pandas.Dataframe.query