简体   繁体   中英

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

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.

在此处输入图片说明

Both scala or python alternatives are welcome! Thanks!!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM