简体   繁体   English

生成一个依赖于该列之前值的列 scala/python

[英]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活跃客户 t = t-1 中的活跃客户 + t 中的招聘 - 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!欢迎使用 scala 或 python 替代品! Thanks!!谢谢!!

You can solve this problem which seems of a cummulative nature with the function .cumsum() :您可以使用函数.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!另外,对于未来的问题,请尝试将您的数据作为文本而不是图片发布!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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