I'm beginner in python and I need to translate some code in R to Python.
I need to find one root per row in a dataset based in a dynamic function, the code in R is:
library(rootSolve
library(dplyr)
library(plyr)
dataset = data.frame(A = c(10,20,30),B=c(20,10,40), FX = c("A+B-x","A-B+x","A*B-x"))
sol<- adply(dataset,1, summarize,
solution_0= uniroot.all(function(x)(eval(parse(text=as.character(FX),dataset))),lower = -10000, upper = 10000, tol = 0.00001))
This code return [30,-10,1200] as a solution for each row.
In python I read a documentation of optimize of sciPy package but i don't found a code that's work for me:
I tried a solutions like that below, but without sucess:
import pandas as pd
from scipy.optimize import fsolve as fs
data = {'A': [10,20,30],
'B': [20,10,40],
'FX': ["A+B-x","A-B+x","A*B-x"]}
df = pd.DataFrame(data)
def func(FX):
return(exec(FX))
fs(func(df.FX),x0=0,args=df)
Someone have idea how to solve this?
Very Thanks.