简体   繁体   English

参考 pandas dataframe 中的列值减去固定行值

[英]Subtract fixed row value in reference to column value in pandas dataframe

I would like to subtract a fixed row value in rows, in reference to their values in another column.我想参考它们在另一列中的值,减去行中的固定行值。

My data looks like this:我的数据如下所示:

       TRACK    TIME  POSITION_X
0        1        0       12       
1        1        30      13       
2        1        60      15       
3        1        90      11      
4        2         0      10     
5        2        20      11
6        2        60      13
7        2        90      17

I would like to subtract a fixed row value (WHEN TIME=0) of the POSITION_X column in reference to the TRACK column, and create a new column ("NEW_POSX") with those values.我想参考 TRACK 列减去 POSITION_X 列的固定行值(WHEN TIME=0),并用这些值创建一个新列(“NEW_POSX”)。 The output should be like this: output 应该是这样的:

       TRACK    TIME  POSITION_X   NEW_POSX
0        1        0       12         0   
1        1        30      13         1
2        1        60      15         3
3        1        90      11        -1
4        2         0      10         0
5        2        20      11         1
6        2        60      13         3
7        2        90      17         7

I have been using the following code to get this done:我一直在使用以下代码来完成这项工作:

import pandas as pd

data = {'TRACK':  [1,1,1,1,2,2,2,2],
        'TIME': [0,30,60,90,0,20,60,90],
        'POSITION_X': [12,13,15,11,10,11,13,17],
        }

df = pd.DataFrame (data, columns = ['TRACK','TIME','POSITION_X'])
df['NEW_POSX']= df.groupby('TRACK')['POSITION_X'].diff().fillna(0).astype(int)
df.head(8)

... but I don't get the desired output. ...但我没有得到想要的 output。 Instead, I get a new column where every row is subtracted by the previous row (according to the "TRACK" column):相反,我得到一个新列,其中每一行都减去前一行(根据“TRACK”列):

       TRACK    TIME  POSITION_X   NEW_POSX
0        1        0       12         0   
1        1        30      13         1
2        1        60      15         2
3        1        90      11        -4
4        2         0      10         0
5        2        20      11         1
6        2        60      13         2
7        2        90      17         4

can anyone help me with this?谁能帮我这个?

You can use transform and first to get the value at time 0, and then substract it to the 'POSITION_X' column:您可以使用transform and first获取时间 0 的值,然后将其减去'POSITION_X'列:

s=df.groupby('TRACK')['POSITION_X'].transform('first')
df['NEW_POSX']=df['POSITION_X']-s

#Same as: 
#df['NEW_POSX']=df['POSITION_X'].sub(s)

Output: Output:

df
   TRACK  TIME  POSITION_X  NEW_POSX
0      1     0          12         0
1      1    30          13         1
2      1    60          15         3
3      1    90          11        -1
4      2     0          10         0
5      2    20          11         1
6      2    60          13         3
7      2    90          17         7

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

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