Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生成一个列,该列取决于该列scala/python的上一个值_Python_Pandas_Scala - Fatal编程技术网

生成一个列,该列取决于该列scala/python的上一个值

生成一个列,该列取决于该列scala/python的上一个值,python,pandas,scala,Python,Pandas,Scala,我需要生成一个列,该列取决于该列的上一个值。公式如下: 活跃客户t=t-1中的活跃客户+t中的雇佣-t中的取消 我拥有的数据集有new hirings和cancelation列,累积有效保单是我想要的输出 欢迎使用scala或python替代方案! 谢谢 您可以通过函数.cumsum(),解决这个似乎具有累加性质的问题: 输出: New Hirings Cancelations Cumulative Active Customers 0 1

我需要生成一个列,该列取决于该列的上一个值。公式如下:

活跃客户t=t-1中的活跃客户+t中的雇佣-t中的取消

我拥有的数据集有new hirings和cancelation列,累积有效保单是我想要的输出

欢迎使用scala或python替代方案!
谢谢

您可以通过函数
.cumsum()
,解决这个似乎具有累加性质的问题:

输出:

   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

此外,对于未来的问题,请尝试将您的数据发布为文本而不是图片

可能重复:谢谢你的回答!!我知道如何基于其他列生成列,但我不知道如何引用同一列的上一个值!我无法使用列的上一个值生成同一列的下一个值。哦,对不起,我没有通读问题。您可以使用
.shift()
切换到上一个值。让我马上编辑我的答案。另外,您可以添加一个示例输出吗?格雷西亚斯!对我的输入是图片中显示的新招聘和取消栏。我需要生成如图所示的“累计活跃客户”列。计算结果为:新员工t-t中的取消+t-1中的累积活跃客户。这有意义吗?可以使用累积值进一步简化计算,如我展示的示例中所示。请让我知道,如果这不起作用,或者你想进一步讨论它,否则请随时接受我的回答!
   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