python - 无法在 jupyter 笔记本单元中第二次加入 pandas 数据框

标签 python pandas jupyter-notebook

我使用 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/

相关文章:

python - 如何使用python(Anaconda)使用vba在指定的单元格位置将图片插入到Excel中

Python使用类似于模拟补丁的技术缓存内部调用

python-3.x - Jupyter 内核在 Ubuntu 上死机

jupyter-notebook - Jupyter Lab不断开启简单模式

Python整数前缀范围算法

Python 正则表达式 - 保持字母字符连续相邻/在数字序列内

python-3.x - 在 Pandas Dataframe 列中查找某些单词,如果找到,将它们添加到新列中

python - 如何使用 smtp 发送 html 电子邮件内嵌的 plot.ly 图像?

python - Matplotlib:绘制数据然后进行时间序列预测

python - Jupyter 笔记本在打开时运行所有单元格