这是我的数据集
Date,p1Close,p2Close,spread,movingAverage
2022-02-28,5,10,2,NaN
2022-03-01,2,6,3,2.5
2022-03-02,4,8,2,2.5
2022-03-03,2,8,4,3
我试图在pandas数据帧中创建一个新列,该列等于前一行和当前行的spread
之间的标准差。
df['standardDeviation'] = statistics.stdev(df['spread'], df['spread'].shift(1))
我一直收到这个错误:
File "/usr/lib/python3.9/statistics.py", line 797, in stdev
var = variance(data, xbar)
File "/usr/lib/python3.9/statistics.py", line 740, in variance
T, ss = _ss(data, xbar)
File "/usr/lib/python3.9/statistics.py", line 684, in _ss
T, total, count = _sum((x-c)**2 for x in data)
File "/usr/lib/python3.9/statistics.py", line 166, in _sum
for n, d in map(_exact_ratio, values):
File "/usr/lib/python3.9/statistics.py", line 248, in _exact_ratio
raise TypeError(msg.format(type(x).__name__))
TypeError: can't convert type 'Series' to numerator/denominator
我相信这是因为我使用的是shift(1)
,在第一次计算时它没有shift(1)
值,所以它会出错。不知道如何解决这个问题。
您实际上可以使用<column>.rolling(2).std()
:
df['standardDeviation'] = df['spread'].rolling(2).std()
输出:
>>> df
Date p1Close p2Close spread movingAverage standardDeviation
0 2022-02-28 5 10 2 NaN NaN
1 2022-03-01 2 6 3 2.5 0.707107
2 2022-03-02 4 8 2 2.0 0.707107
3 2022-03-03 2 8 4 3.0 1.414214
@richardec的答案是最好的解决方案,但对于您的特定问题,statistics.stdev
需要一个迭代器,因此您需要成对传递:
df['stdev'] = [statistics.stdev(pair) for pair in zip(df['spread'], df['spread'].shift())]
输出:
Date p1Close p2Close spread movingAverage stdev
0 2022-02-28 5 10 2 NaN NaN
1 2022-03-01 2 6 3 2.5 0.707107
2 2022-03-02 4 8 2 2.0 0.707107
3 2022-03-03 2 8 4 3.0 1.414214