1

we have a data frame containing four features of dtype; num, num, object and float respectively.

And i want to filter data with this condition:
1. f['g']=< 53
2. f['h'] minmax (1,67) ..the value should be between the user-specified value.
3. f['k]=='y'
4.f['l'] minmax(22,77) ..the value should be between the user-specified value.

I want to write a function named 'Datafilter' which can do multiple filtering and also we should be able to add filters and change conditions according to our future requirement.

Here is a Dataframe.

f= pd.DataFrame({'g':[1,2,3,45,33,57,23,53,56,79],   
'h':[12,344,5,78,98,95,43,56,32,23],
'k':['y','y','n','y','n','n','y','n','y','n'],
'l':[1.0,2.0,22.0,244.0,43.0,34.0,78.0,5.0,77.0,97.0]})

I am a beginner so I was able to do it by applying one filter at a time.

>>>[IN] t=f.copy()

>>>[IN] t=t[ t['g']<= 53]

>>>[IN] t=t[t['h'].between(12,98) & t['l'].between(2.0,78.0)]

>>>[IN] t=t[t['k']=='n']

>>>[IN] print(t)

>>>[OT] 

    g   h   k   l
4   33  98  n   43.0
7   53  56  n   5.0

the function should be generic i.e. reusable and should able to handle additional filters as well, in the future.

0

2 Answers 2

1

Just use the & operator to apply all filters at the same time just as in

t=t[(t['h'].between(12,98)) & (t['l'].between(2.0,78.0))].

You have to put each filter into round brackets else there will be an error.

Sign up to request clarification or add additional context in comments.

Comments

0

You can seimply do something like this.

from typing import *
import pandas as pd

f= pd.DataFrame({'g':[1,2,3,45,33,57,23,53,56,79, 12],   
'h':[12,344,5,78,98,95,43,56,32,23, 33],
'k':['y','y','n','y','n','n','y','n','y','n', 'y'],
'l':[1.0,2.0,22.0,244.0,43.0,34.0,78.0,5.0,77.0,97.0,66]})

def Datafilter(df, h_minmax : List[int], l_minmax : List[int]) -> pd.DataFrame:
    return df[(df["g"] <= 53) & (df["h"].between(*h_minmax)) & (df["l"].between(*l_minmax)) & (df["k"] == "y")]

print(Datafilter(f, [1,67], [22,77]))

That will give you.

     g  h   k   l
10  12  33  y   66
``

Comments

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.