Pandas: building a column with self-assessment of past values

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?

+5
source share
1 answer

pandas does not have a great way to handle general recursive computing. There might be some kind of vectorization trick, but if you can take the addiction, it is relatively painless and very fast with numba .

 @numba.njit def make_b(a): b = np.zeros_like(a) b[0] = 1 for i in range(1, len(a)): b[i] = min(b[i-1], a[i-1]) + 2 return b df['b'] = make_b(df['a'].values) df Out[73]: 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 
+5
source

Source: https://habr.com/ru/post/1258222/


All Articles