1

I have a dataframe of the following format:

Name           Factor     Expression   Year 
Hydro          0.075            <10    2010  
Hydro          0.075            >10    2010  
Hydro          0.075            <10    2011  
Hydro          0.075            >10    2011
Hydro          0.075            <10    2012

And the following variable: i=3.

I would like to filter the dataframe where the Expression column evaluates as true, when the variable i is on the left hand side of the string in the column of expression.

For example, the first row would evaluate to true as 3<10.

The resulting dataframe that I would like is:

Name           Factor     Expression   Year 
Hydro          0.075            <10    2010  
Hydro          0.075            <10    2011  
Hydro          0.075            <10    2012

Thank you for any help.

1 Answer 1

2

Pandas has a safer version of eval that supports a limited number of operations. Luckily, > and < work, and you can use this along with string concatenation:

i = '3'
idx = pd.eval(i + df.Expression)
df.loc[idx]

    Name  Factor Expression  Year
0  Hydro   0.075        <10  2010
2  Hydro   0.075        <10  2011
4  Hydro   0.075        <10  2012

As @coldspeed noted, the above approach only works on DataFrames that are < 100 rows*, which isn't ideal. He also proposed the following solution:

df[[pd.eval(f"{i}{j}") for j in df['Expression']]]

*The above limitation is discussed more in depth in the following question: AttributeError: 'PandasExprVisitor' object has no attribute 'visit_Ellipsis', using pandas eval

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

5 Comments

Careful, pd.eval will only work if your DataFrame is 100 rows or smaller.
Hmm, did not know that, really limits things quite a bit
If you want to generalise to any size, you should use df[[pd.eval(f"{i}{j}") for j in df['Expression']]] instead.
@coldspeed do you know where that limitation is documented? I just get an attribute error when it reaches that size and I don't see it in the documentation
Yes! It is documented here: stackoverflow.com/questions/48008191/…

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.