Skip to content
Advertisement

Solving formula across multiple rows (similar to excel) in python?

I’m looking for some kind of functionality similar to excels solver in python. In python I have a function which when supplied an array of length N, returns an array of N also using some columns from a dataframe.

Below is a simple example of that I have, and what the target is.

JavaScript

From here I am looking for a function/method that I can supply ‘funs’ to and a target array. The function hopefully will generate the x such that funs(x) = target.

JavaScript

An easier approach in this case would be to define the outcome such that x = target/(col_1 * col_2 * col_3), but a solution like this isn’t as trivial in the real example, hence why I wonder if something similar to how excel solver would work exists.

Hope this makes sense and I really appreciate any help.

Advertisement

Answer

The function scipy.optimize.fsolve finds zeros of functions, which can be used in your case as follows:

JavaScript

This results in sol = array([0.1, 0.15625, 0.27777778, 0.3125, 0.5]).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement