Reputation: 43
I am trying to add a column to a pandas dataframe
import pandas as pd
df = pd.DataFrame([['a',1],['b',0],['c',1],['d',1],['e',0],['f',1]])
such that it contains the result of a cumulative custom function
a --> (total + a) * a
that is, it takes the value a, sums it up with the total and multiplies the result. In my example I would like to have as output:
pd.DataFrame([['a',1,1],['b',0,0],['c',1,1],['d',1,2],['e',0,0],['f',1,1]])
I understand that this could be done using
df.expanding.apply(some_lambda_function)
but I have some difficult in understanding how to code it.
Do you have any idea?
many thanks.
Upvotes: 2
Views: 166
Reputation: 323306
I will recommend for loop ..
start=0
total=[]
for x ,y in df.iterrows():
start=(y[1]+start)*y[1]
total.append(start)
total
Out[201]: [1, 0, 1, 2, 0, 1]
Upvotes: 1