Search code examples
pythonpandasdataframeapplydata-wrangling

Apply a function to dataframe which includes previous row data


I have an input dataframe for daily fruit spend which looks like this:

spend_df

Date        Apples      Pears      Grapes     
01/01/22      10         47          0
02/01/22      0          22          3
03/01/22      11         0           3
...

For each fruit, I need to apply a function using their respective parameters and inputs spends. The function includes the previous day and the current day spends, which is as follows:

y = beta(1 - exp(-(theta*previous + current)/alpha))

parameters_df

Parameter    Apples      Pears      Grapes  
alpha         132         323        56
beta          424         31         33
theta         13          244        323

My output data frame should look like this (may contain errors):

profit_df

Date         Apples        Pears       Grapes     
01/01/22      30.93         4.19        0       
02/01/22      265.63        31.00       1.72
03/01/22      33.90         30.99       32.99
...     

This is what I attempted:

# First map parameters_df to spend_df
merged_df = input_df.merge(parameters_df, on=['Apples','Pears','Grapes'])

# Apply function to each row
profit_df = merged_df.apply(lambda x: beta(1 - exp(-(theta*x[-1] + x)/alpha))

Solution

  • It might be easier to read if you extract the necessary variables from parameters_df and spend_df first. Then a simple application of the formula will produce the expected output.

    # extract alpha, beta, theta from parameters df
    alpha, beta, theta = parameters_df.iloc[:, 1:].values
    # select fruit columns
    current = spend_df[['Apples', 'Pears', 'Grapes']]
    # find previous values of fruit columns
    previous = current.shift(fill_value=0)
    
    # calculate profit using formula
    y = beta*(1 - np.exp(-(theta*previous + current) / alpha))
    profit_df = spend_df[['Date']].join(y)
    

    res