Apply an equation to a dataframe using coefficients from another dataframe

Question:

I have to 2 dataframes:

input_df

Apples    Pears   Peaches   Grapes
12        23       0         4
10        0        0         4
12        16       12        5
6         0        0         11

coefficients_df

Fruit       n    w1      w2
Apples      2    0.4     40
Pears       1    0.1     43
Peaches     1    0.6     51
Grapes      2    0.5     11

I’m trying to apply an equation y = w2*(1-exp(-w1*input_df^n))to input_df. The equation takes coefficients from coefficients_df

This is what I tried:

# First map coefficients_df to input_df
merged_df = input_df.merge(coefficients_df.pivot('Fruit'), on=['Apples','Pears','Peaches','Grapes'])

# Apply function to each row
output_df = merged_df.apply(lambda x: w2*(1-exp(-w1*x^n))
Asked By: star_it8293

||

Answers:

Use broadcasting to get value from df_input:

# df1 is input_df
# df2 is coefficients_df
col_idx = (df1.columns.to_numpy() == df2['Fruit'].to_numpy()[:, None]).argmax(axis=1)
x = df1.values[df1.index, col_idx]

y = df2['w2']*(1-np.exp(-df2['w1']*x**df2['n']))

Output:

>>> y
0    40.000000
1     0.000000
2    50.961924
3    11.000000
dtype: float64
Answered By: Corralien

Use simple index alignment:

coeff = coefficients_df.set_index('Fruit')

y = coeff['w2']*(1-np.exp(-coeff['w1']*input_df**coeff['n']))

Output:

      Apples     Pears    Peaches     Grapes
0  40.000000  38.68887   0.000000  10.996310
1  40.000000   0.00000   0.000000  10.996310
2  40.000000  34.31845  50.961924  10.999959
3  39.999978   0.00000   0.000000  11.000000
Answered By: mozway

Im a bit late to the party but here you go.

import numpy as np
results = {}
for col in input_df.columns:
    coefs = coefficients_df.loc[coefficients_df['Fruit'] == col].iloc[0,1:]
    y = np.array(coefs.w2*(1-np.exp(-coefs.w1*(input_df[col]**coefs.n))))
    results[col] = y

and for me the output is

{'Apples': array([40.       , 40.       , 40.       , 39.9999777]),
 'Pears': array([38.68886972,  0.        , 34.31844973,  0.        ]),
 'Peaches': array([ 0.        ,  0.        , 50.96192412,  0.        ]),
 'Grapes': array([10.99630991, 10.99630991, 10.99995901, 11.        ])}
Answered By: batuhaneralp