Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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
Python 如何使用Pandas基于另一列值累计添加或减去值?_Python_Pandas_Cumulative Sum - Fatal编程技术网

Python 如何使用Pandas基于另一列值累计添加或减去值?

Python 如何使用Pandas基于另一列值累计添加或减去值?,python,pandas,cumulative-sum,Python,Pandas,Cumulative Sum,我在下面有一个数据框,显示基于秒的电压输出。v_out值基于+/-0.05厘米的位移 因此,当v_out变得更正值时,与上一个v_out值相比,存在正位移。当v_out变得更负时,位移朝-方向移动 我有一个初始的df,我想添加一个sign列,根据前面的v\u out值来判断v\u out是正的还是负的。而且,我需要一个累计列,它跟踪符号列的新增运行总数 首字母df secs v_out 0 0.0 -1.179100 1 15.0 -1.179100 2 1

我在下面有一个数据框,显示基于秒的电压输出。
v_out
值基于+/-0.05厘米的位移

因此,当
v_out
变得更正值时,与上一个
v_out
值相比,存在正位移。当
v_out
变得更负时,位移朝
-
方向移动

我有一个初始的
df
,我想添加一个
sign
列,根据前面的
v\u out
值来判断
v\u out
是正的还是负的。而且,我需要一个
累计
列,它跟踪
符号
列的新增运行总数

首字母
df

     secs     v_out
0     0.0 -1.179100
1    15.0 -1.179100
2    18.0 -1.179200
3    33.0 -1.181800
4    48.0  0.029461
我想要什么

     secs     v_out  sign  cumul
0     0.0 -1.179100  0.00  0.00
1    15.0 -1.179100  0.00  0.00
2    18.0 -1.179200 -0.05 -0.05
3    33.0 -1.181800 -0.05 -0.10
4    48.0  0.029461  0.05 -0.05
查看滞后值的方法命名为
shift
,然后我们使用if-else构造检查值是正、负还是零

因此,首先我们要构造列
符号
。逻辑可以打包成一行

df['sign'] = (df.v_out - df.v_out.shift()).apply(lambda x: 0.05 if x > 0 else -0.05 if x < 0 else 0)
最终df如下所示:

   secs     v_out  sign  cumul                                                                                      
0   0.0 -1.179100  0.00   0.00                                                                                          
1  15.0 -1.179100  0.00   0.00                                                                                          
2  18.0 -1.179200 -0.05  -0.05                                                                                          
3  33.0 -1.181800 -0.05  -0.10                                                                                          
4  48.0  0.029461  0.05  -0.05`   

您好,谢谢您的帮助,虽然
cumul
输出中有一行从
-5e-02
到下一行,即
-1.3877e-17
,这对我来说毫无意义。
-1.3877e-17
非常接近
0
。您可能看到一些浮点错误。使用
numpy.round
仅保留小数点后两位。没有更多的信息,我不能提供任何其他建议。
   secs     v_out  sign  cumul                                                                                      
0   0.0 -1.179100  0.00   0.00                                                                                          
1  15.0 -1.179100  0.00   0.00                                                                                          
2  18.0 -1.179200 -0.05  -0.05                                                                                          
3  33.0 -1.181800 -0.05  -0.10                                                                                          
4  48.0  0.029461  0.05  -0.05`