1

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.

1

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

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

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

2 Comments

shouldn't it be like this: filtered_df = [df[df[col] == val] for val in filter_vals]? current code will produce a syntax error
yes, I forgot the brackets, added them now

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.