v_coder12
v_coder12

Reputation: 180

pythonic way to apply multiple filters to a dataframe based on user input

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.

Upvotes: 1

Views: 296

Answers (2)

user14776666
user14776666

Reputation:

filter_vals = [1, 2, 3]
filter_amount = 3

filtered_df = [df[df[col] == val] for val in filter_vals] 

Upvotes: 0

Zishaan Sunderji
Zishaan Sunderji

Reputation: 186

Since the filter performs an and (&), it would make sense to do it like this:

import pandas as pd

def filter_data(df, filter_col, filter_val, filter_amount):
    out = df.copy()
    for i in range(filter_amount):
        out = out[out[filter_col[i]] == filter_val[i]]
    return out

def main():
    x = pd.DataFrame({"Age": [12, 44, 23], "Ethnicity": ["White", "Black", "White"], "Height": [180, 182, 168]})
    #    Age Ethnicity  Height
    # 0   12     White     180
    # 1   44     Black     182
    # 2   23     White     168

    y = filter_data(x, ["Ethnicity", "Height"], ["White", 180], 1)
    #    Age Ethnicity  Height
    # 0   12     White     180
    # 2   23     White     168

    z = filter_data(x, ["Ethnicity", "Height"], ["White", 180], 2)
    #    Age Ethnicity  Height
    # 0   12     White     180

Upvotes: 1

Related Questions