Generate a Column that depends on the previous value of that column scala/python

Viewed 57

I need to generate a column that depends on the previous value of that column. The formula would be something like this:

active customers t = actives customers in t-1 + hires in t - cancelations in t

The data set I have has the new hirings and cancelation column and the cumulative active policies is my desired output.

enter image description here

Both scala or python alternatives are welcome! Thanks!!

1 Answers

You can solve this problem which seems of a cummulative nature with the function .cumsum():

df['active customers t'] = (df['New Hirings'] - df['Cancelation']).cumsum()

Output:

   New Hirings  Cancelations  Cumulative Active Customers
0            1             1                            0
1            1             0                            1
2            2             0                            3
3            2             0                            5
4            5             1                            9
5            0             1                            8
6            7             0                           15
7            2             3                           14
8            0             2                           12
9            2             1                           13

Also, for future issues, please try to post your data as a text and not a picture!

Related