Home > Software engineering >  Passing a string to pandas.loc
Passing a string to pandas.loc

Time:11-26

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?

CodePudding user response:

You need to use eval :

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

subset_frame= data_frame.loc[eval(mystring)]
  • Related