Passing a string to pandas.loc

Question:

There is a pandas data frame for which it is required make a subset using multiple conditions. This works when the conditions are hard-coded:

subset_frame = data_frame.loc[(data_frame['Quantity'] >5) & (data_frame['Discount'] >0)]

The conditions vary and a function is being created for whatever list of conditions is supplied. A string is produced by the function:

mystring = "(data_frame['Quantity'] >5) & (data_frame['Discount'] >0)"

This is fed into .loc:

subset_frame= data_frame.loc[mystring]

Which returns an error:

KeyError: "(data_frame['Quantity'] >5) & (data_frame['Discount'] >0)"

As per first example with subset_frame above, this exact text, copied from the KeyError output and pasted/hard-coded into .loc, runs successfully.

A different method, .update, has also been attempted:

data_frame.update(data_frame.loc[mystring])

This returns the same error.

What is the mistake in my code or my approach?

Asked By: acolls_badger

||

Answers:

You need to use eval :

mystring = "(data_frame['Quantity'] >5) & (data_frame['Discount'] >0)"

subset_frame= data_frame.loc[eval(mystring)]
Answered By: abokey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.