英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论