3

I am filtering a DataFrame inside a function. I get multiple conditions as string arguments which I have to employ to filter the DataFrame. exec() usually helps to execute a string expression, but I am not able to fine tune it for the DataFrame.

Here is a small miniature example:

import pandas as pd
df = pd.DataFrame({'A':[30,60,90,40],'B':['X','X','X','Y']})
print(df)
      A  B
  0  30  X
  1  60  X
  2  90  X
  3  40  Y

Now, the code below gives an error naturally. My question is how we can evaluate this string expression in the function below like exectable(expression) and get the filtered DataFrame:

def func(df,cond1,cond2):
    expression = "(df.A "+cond1+") & (df.B"+cond2+")"
    print(expression) # This results in --> (df.A < 50) & (df.B == 'X')

    return df[expression]
func(df,"< 50"," == 'X'")

Desired output,

      A  B
  0  30  X
2
  • 3
    Have you tried eval? Commented Oct 2, 2020 at 12:40
  • It works. Many thanks. Just post one line answer and I will accept it. Since exec() did not work, so I couldn't forsee that eval() would work. Commented Oct 2, 2020 at 12:42

2 Answers 2

1

eval is what you are looking for.

def func(df,cond1,cond2):
    expression = eval(f"(df.A {cond1}) & (df.B{cond2})")

    return df[expression]
Sign up to request clarification or add additional context in comments.

1 Comment

True, but it exists and so has applications. Unless TS is not putting user's input into eval he is ok.
1

Youi can use df.query:

import pandas as pd

def func(df,cond1,cond2):
    expression = "(A "+cond1+") & (B"+cond2+")"
    return df.query(expression)

df = pd.DataFrame({'A':[30,60,90,40],'B':['X','X','X','Y']})
print( func(df,"< 50"," == 'X'") )

Prints:

    A  B
0  30  X

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.