Search code examples
pythonpandasdataframeaggregate

first order dynamic process using a pandas aggregate


I'd like to be able to create the following Data Frame in pandas

A B C
a1 b1 c1
a2 b2 = f(c1) c2 = a2 + b2
a3 b3 = f(c2) c3 = a3 + b3

Ahead of time I know the A column, b1 and c1 are initial conditions, and f is a known function. The ith row of column is b_i = f(c_{i-1}) and c_i = a_i + b_i. All the a_i, b_i, c_i are floats.

I can do it using a for loop, but I'd love to be able to do this using pandas rolling/apply so I can scale it up and insert a groupby. I can't for the life of me figure it out though. Any clues?


Solution

  • import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, np.nan, np.nan], 'C': [1, np.nan, np.nan]})
    
    print(df)
    
    def my_func(x):
        df.loc[x.index, 'B'] = df.loc[x.index - 1, 'C'].values[0]
        df.loc[x.index, 'C'] = df.loc[x.index, 'A'].values[0] + df.loc[x.index, 'B'].values[0]
    
    
    df[1:].groupby(df.index[1:]).apply(my_func)
    
    print(df)
    

    Input

       A    B    C
    0  1  1.0  1.0
    1  2  NaN  NaN
    2  3  NaN  NaN
    

    Output

       A    B    C
    0  1  1.0  1.0
    1  2  1.0  3.0
    2  3  3.0  6.0