Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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 从一列中获取值作为减法变量_Python_Pandas - Fatal编程技术网

Python 从一列中获取值作为减法变量

Python 从一列中获取值作为减法变量,python,pandas,Python,Pandas,我有一个带有XY和距离的数据框。我试图做的是将距离存储为变量,如果X或Y的值大于0,则从下一个距离中减去它 这是一个示例df dist x y 0 12.93 99.23 200 0 0 400 0 0 600 0 0 800 0 0 1000 12.46 99.14 1200 0 0 1400 0 0 1600 0

我有一个带有XY和距离的数据框。我试图做的是将距离存储为变量,如果X或Y的值大于0,则从下一个距离中减去它

这是一个示例df

dist     x      y
  0    12.93   99.23
200     0        0
400     0        0
600     0        0
800     0        0
1000    12.46   99.14
1200     0        0
1400     0        0
1600     0        0
1800     0        0
2000    12.01   99.07
这是新的df

dist     x      y
  0    12.93   99.23
200     0        0
400     0        0
600     0        0
800     0        0
  0    12.46   99.14
 200     0        0
 400     0        0
 600     0        0
 800     0        0
2000    12.01   99.07 
最后一个值无关紧要,但从技术上讲,它应该是0

其思想是,在每个已知XY处,将距离指定为0,然后减去该距离,直到下一个已知XY 在上面的例子中,距离是四舍五入的数字,但实际上,它们可能是

132.05
19.999
1539.65

依此类推

您可以使用
groupby
apply
,使用自定义的grouper,计算如下:

grouper = (df['x'].ne(0) | df['y'].ne(0)).cumsum()
df['dist'].groupby(grouper).apply(lambda x: x - x.values[0])

0       0
1     200
2     400
3     600
4     800
5       0
6     200
7     400
8     600
9     800
10      0
Name: dist, dtype: int64
在哪里,

grouper

0     1
1     1
2     1
3     1
4     1
5     2
6     2
7     2
8     2
9     2
10    3
dtype: int64

其思想是标记所有必须从相应组的第一个非零值中减去的行。

使用
其中
+
ffill

df['dist'] = df.dist - df.where(df.x.gt(0) | df.y.gt(0)).dist.ffill()

     dist      x      y
0     0.0  12.93  99.23
1   200.0   0.00   0.00
2   400.0   0.00   0.00
3   600.0   0.00   0.00
4   800.0   0.00   0.00
5     0.0  12.46  99.14
6   200.0   0.00   0.00
7   400.0   0.00   0.00
8   600.0   0.00   0.00
9   800.0   0.00   0.00
10    0.0  12.01  99.07

使用
transform

df.dist-=df.groupby(df.x.ne(0).cumsum())['dist'].transform('first')
df
Out[769]: 
    dist      x      y
0      0  12.93  99.23
1    200   0.00   0.00
2    400   0.00   0.00
3    600   0.00   0.00
4    800   0.00   0.00
5      0  12.46  99.14
6    200   0.00   0.00
7    400   0.00   0.00
8    600   0.00   0.00
9    800   0.00   0.00
10     0  12.01  99.07