Home > front end >  Pandas: How to evalueate equations in a column
Pandas: How to evalueate equations in a column

Time:05-11

Consider the following table:

Obj   Bits    Eq            Result
A     5       5*Bits 20     45
B     10      7*Bits 8      78
C     11      12*Bits 100   232

I would like to create a column Result which evaluates the expression in column Eq. These equations are not fixed and can vary per row. However, the variables used in the equation will be one or more column names in the dataframe. How do I go about accomplishing this? I know Pandas has the eval method but in my case the equation per row can be different. Thanks for any ideas.

CodePudding user response:

A solution:

df["Result"] = df.apply(lambda x : eval(x["Eq"].replace(r"Bits", str(x["Bits"]))),axis=1)

Here I interpolate the values of bits in the equation column. So for every row. the string replaces "Bits" in the equation with its the value that can be found in the Bits column.

Now, we have something like "5*2 3" and we can wrap the expression in eval which evaluates the call as if it were Python code.

This is dangerous because arbitrary Python code can be executed. For example, it could run eval("Destroy everything I hold dear") see here and execute it, no questions asked. If you control the entire chain of creation of the equation string, this should not be an issue but if it comes from somewhere you didn't vet, it might be dangerous.

Another solution would be using sympy equations in the column and use the other column as a symbol then execute the result and store it in the last column. This is probably safer and more robust to more complex equations as well but it adds a dependency to your code (and you have to learn a new package).

  • Related