我使用 pandas.DataFrame.join
函数向我的数据帧添加两个新的 SMA
列。
第一次工作正常,但如果我再次运行该单元,我会收到以下错误:
ValueError:列重叠但未指定后缀:Index(['SMA_LONG'], dtype='object')
以下是我的代码。
#cell 1
import numpy as np
import pandas as pd
#cell 2
df = pd.DataFrame({
'close': np.random.uniform(0.1,0.9, 100),
})
#cell 3
SMA_long = 12
SMA_short = 7
sma_long = df['close'].rolling(window=SMA_long, min_periods=SMA_long - 1).mean()
df = df.join(sma_long.to_frame('SMA_LONG'))
sma_short = df['close'].rolling(window=SMA_short, min_periods=SMA_short - 1).mean()
df = df.join(sma_short.to_frame('SMA_SHORT'))
df.tail()
我该如何解决这个问题?
谢谢。
最佳答案
您的错误意味着 df = df.join(sma_long.to_frame('SMA_LONG'))
之前已经存在 SMA_LONG
。
对我来说,您的示例数据解决方案工作得很好,但我认为您可以通过分配到新列来简化它:
SMA_long = 12
SMA_short = 7
df['SMA_LONG'] = df['close'].rolling(window=SMA_long, min_periods=SMA_long - 1).mean()
df['SMA_SHORT'] = df['close'].rolling(window=SMA_short, min_periods=SMA_short - 1).mean()
print (df)
close SMA_LONG SMA_SHORT
0 0.649439 NaN NaN
1 0.332926 NaN NaN
2 0.492527 NaN NaN
3 0.500444 NaN NaN
4 0.583334 NaN NaN
.. ... ... ...
95 0.775169 0.532850 0.577613
96 0.470479 0.524123 0.594733
97 0.237417 0.517054 0.512506
98 0.753701 0.536595 0.554372
99 0.133795 0.526603 0.475595
[100 rows x 3 columns]
或者:
sma_long = df['close'].rolling(window=SMA_long, min_periods=SMA_long - 1).mean()
sma_short = df['close'].rolling(window=SMA_short, min_periods=SMA_short - 1).mean()
df = df.assign(SMA_LONG=sma_long, SMA_SHORT=sma_short)
关于python - 无法在 jupyter 笔记本单元中第二次加入 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490846/