Rahul Chandran
Rahul Chandran

Reputation: 31

How to add previous output values as input values in python dataframe

I have 3 dataFrames a,b, and c.I want to calculate the sum of a+b+previous output(c).

a=pd.DataFrame([1,2,3,4])
b=pd.DataFrame([1,2,3,4])
c=pd.DataFrame([])
c=c.append(a+b+c.shift(1))

Here I have already to dataframes a and b ,then i created a new dataframe c for saving output of the calculation."c.shift(1) means the previous output.

It shows error.How it is possible??

Upvotes: 1

Views: 148

Answers (1)

Gil Pinsky
Gil Pinsky

Reputation: 2493

Appending will only work in a loop and will not return a value so you could do it but your computation can be reduced to cumsum in this case:

a=pd.DataFrame([1,2,3,4])
b=pd.DataFrame([1,2,3,4])
c=(a+b).cumsum()

Upvotes: 2

Related Questions