I have a df
:
pd.DataFrame(index = ['A','B','C'],
columns = ['1','2','3','4'],
data = [[100,60,40,60],
[200,10,50,80],
[50, np.nan, np.nan, np.nan]])
1 2 3 4
A 100 60 40 60
B 200 10 50 80
C 50
I would like to calculate the remaining C
index values, but each calculation is dependent on the previous value like so:
1 2 3 4
A 100 60 40 60
B 200 10 50 80
C 50 A2+B2-C1 A3+B3-C2 A4+B4-C3
I checked this answer and tried the following:
new = [df.loc['C'].values]
for i in range(1, len(df.index)):
new.append(new[i-1]*df.loc['A'].values[i]+df.loc['B'].values[i]-df.loc['C'].values[i-1])
df.loc['C'] = new
But I get :
ValueError: cannot set a row with mismatched columns
Also, the question and answers are quite outdated, maybe there is a new solution for these recursive functions inside pandas dataframe?