连续行之间的差距跳过两行

huangapple go评论48阅读模式
英文:

Difference of consecutive rows skip two

问题

我需要获取两个连续行的差异,并将结果存储在一个新的列dA中,类似于以下方式:

gb['dA'] = gb['pBidPx'] - gb['pAskPx'].shift(-1)

但显然这不起作用,因为看看上面的dA列。我需要它看起来像这样:

          stkPx  strike     delta trade_date  expirDate      dte  cType  pBidPx  pAskPx    dA
2     86.91    76.0  0.893083 2008-12-29 2009-01-17  0.05205      1    0.50    0.56  N/A
3     86.91    77.0  0.877667 2008-12-29 2009-01-17  0.05205      1    0.60    0.66  0.04  #.60 - .56
4     86.91    78.0  0.859326 2008-12-29 2009-01-17  0.05205      1    0.71    0.76  .05 #.71 - .66 
5     86.91    79.0  0.838882 2008-12-29 2009-01-17  0.05205      1    0.85    0.91  .09 # .85 - .76
英文:

I have a DataFrame that looks like this:

      stkPx  strike     delta trade_date  expirDate      dte  cType  pBidPx  pAskPx    dA
2     86.91    76.0  0.893083 2008-12-29 2009-01-17  0.05205      1    0.50    0.56 -0.16
3     86.91    77.0  0.877667 2008-12-29 2009-01-17  0.05205      1    0.60    0.66 -0.16
4     86.91    78.0  0.859326 2008-12-29 2009-01-17  0.05205      1    0.71    0.76 -0.20
5     86.91    79.0  0.838882 2008-12-29 2009-01-17  0.05205      1    0.85    0.91 -0.21

I need to take the difference of two consecutive rows, store the result in a new column dA and store the result in the second of the two consecutive rows, sort of like this:

gb['dA'] = gb['pBidPx'] - gb['pAskPx'].shift(-1)

But clearly this doesn't work as look at the dA column above. I need it to look like this:

      stkPx  strike     delta trade_date  expirDate      dte  cType  pBidPx  pAskPx    dA
2     86.91    76.0  0.893083 2008-12-29 2009-01-17  0.05205      1    0.50    0.56  N/A
3     86.91    77.0  0.877667 2008-12-29 2009-01-17  0.05205      1    0.60    0.66  0.04  #.60 - .56
4     86.91    78.0  0.859326 2008-12-29 2009-01-17  0.05205      1    0.71    0.76  .05 #.71 - .66 
5     86.91    79.0  0.838882 2008-12-29 2009-01-17  0.05205      1    0.85    0.91  .09 # .85 - .76

答案1

得分: 3

你应该进行正向shift,而不是负向:

gb['dA'] = gb['pBidPx'] - gb['pAskPx'].shift()

输出:

   stkPx  strike     delta trade_date  expirDate      dte  cType  pBidPx  pAskPx    dA
2  86.91    76.0  0.893083 2008-12-29 2009-01-17  0.05205      1    0.50    0.56   NaN
3  86.91    77.0  0.877667 2008-12-29 2009-01-17  0.05205      1    0.60    0.66  0.04
4  86.91    78.0  0.859326 2008-12-29 2009-01-17  0.05205      1    0.71    0.76  0.05
5  86.91    79.0  0.838882 2008-12-29 2009-01-17  0.05205      1    0.85    0.91  0.09
英文:

You should shift positively, not negatively:

gb['dA'] = gb['pBidPx'] - gb['pAskPx'].shift()

Output:

   stkPx  strike     delta trade_date  expirDate      dte  cType  pBidPx  pAskPx    dA
2  86.91    76.0  0.893083 2008-12-29 2009-01-17  0.05205      1    0.50    0.56   NaN
3  86.91    77.0  0.877667 2008-12-29 2009-01-17  0.05205      1    0.60    0.66  0.04
4  86.91    78.0  0.859326 2008-12-29 2009-01-17  0.05205      1    0.71    0.76  0.05
5  86.91    79.0  0.838882 2008-12-29 2009-01-17  0.05205      1    0.85    0.91  0.09

huangapple
  • 本文由 发表于 2023年5月14日 23:34:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定