I need to create a column that starts with an initial value, and then is generated by a function that includes the past values โโof this column. for instance
df = pd.DataFrame({'a': [1,1,5,2,7,8,16,16,16]}) df['b'] = 0 df.ix[0, 'b'] = 1 df ab 0 1 1 1 1 0 2 5 0 3 2 0 4 7 0 5 8 0 6 16 0 7 16 0 8 16 0
Now I want to generate the rest of column "b" by taking the minimum of the previous row and adding two. One solution would be
for i in range(1, len(df)): df.ix[i, 'b'] = df.ix[i-1, :].min() + 2
The result in the desired output.
ab 0 1 1 1 1 3 2 5 3 3 2 5 4 7 4 5 8 6 6 16 8 7 16 10 8 16 12
Does pandas have a "clean" way to do this? Preferably the one that will vectorize the calculation?
source share