Solve on equation/expression on multiple parameters

Viewed 101

I have the following dataframe:

  ID Model_form     A      C     Actual
  1  Exp           2     1.4       4
  2  Power model   1     0.2       3
  3  Log           0.6   6         7

I am trying to solve different equations based on the model form:

 If model form column contains 'exp' - A*(1-exp(C*actual))
 If model form column contains 'pow' - A*(actual^C)
 If model form column contains 'log' - A* Ln(1+C*optimal)

Currently I am solving this as follows,

   c1 = df['Model_form']].str.contains('exp', flags = re.IGNORECASE)
   c2 = df['Model_form']].str.contains('pow', flags = re.IGNORECASE)
   c3 = df['Model_form']].str.contains('log', flags = re.IGNORECASE)

   df['Actual(y)'] = np.select([c1,c2,c3], [df.eval(df['A']*(1-
   np.exp(df['C']*df['Actual'])),df['A']* 
   (df['Actual']**df['C']),df['A']*np.log(1+df['C']*df['Actual']))])

I get the error:

   eval() takes from 2 to 3 positional arguments but 4 were given
1 Answers
c1 = df['Model_form'].str.contains('exp', flags = re.IGNORECASE)
c2 = df['Model_form'].str.contains('pow', flags = re.IGNORECASE)
c3 = df['Model_form'].str.contains('log', flags = re.IGNORECASE)

labels=[df.eval(df['A']*(1-np.exp(df['C']*df['Actual']))),df.eval("A*(Actual**C)"),df.eval(df['A']*np.log(1+df['C']*df['Actual']))]

Finally:

df['Actual(y)']=np.select([c1,c2,c3],labels)

output of df:

    ID  Model_form      A       C      Actual   Actual(y)
0   1   Exp             2.0     1.44    4       -632.696658
1   2   Power model     1.0     0.20    3.0     1.245731
2   3   Log             0.6     6.00    7.0     2.256720

Note: There is no point of using df.eval() in 1st and 3rd condition because alone df['A']*(1-np.exp(df['C']*df['Actual'])) and df['A']*np.log(1+df['C']*df['Actual']) are giving you your desired output and df.eval() is doing nothing(except in condition 2) !!

you are getting this error:

 eval() takes from 2 to 3rd positional arguments but 4 were given

Due to the missing parenthesis ) in 1stcondition:

df.eval(df['A']*(1-np.exp(df['C']*df['Actual'])))
                                                ^              
                                         #added ) parenthesis
Related