为了账号安全,请及时绑定邮箱和手机立即绑定

在pycharm中使用Jupyter notebook命令时出现Key error 5问题

在pycharm中使用Jupyter notebook命令时出现Key error 5问题

一只名叫tom的猫 2023-06-06 14:45:44
我偶然发现了一个我不理解或找不到解决方案的问题。我键入的代码在我的 Jupyter Notebook 中运行完美,但在我使用 PyCharm 时出现错误。我在 PyCharm 中输入了这些代码:df_ticker.loc[df_ticker['Red'] == True, 'Value'] = df_ticker['Open'] - df_ticker['Close']df_ticker.loc[df_ticker['Red'] == False, 'Value'] = df_ticker['Close'] - df_ticker['Open']单独使用这些代码并打印它们的值是成功的。H但是,当与这些代码行匹配时:for i in range(1, len(df_ticker)):if df_ticker.Close[i] > df_ticker.Close[i-1] and df_ticker.Red[i] == False and \        df_ticker.Value.round(decimals = 0)[i] >= df_ticker.Value.round(decimals = 0)[i-1] \        and not df_ticker.Close[i-1] >= df_ticker [i-2]:    rise.append(df_ticker.Close[i])它返回此错误:Traceback (most recent call last):  File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2889, in get_loc    return self._engine.get_loc(casted_key)  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc  File "pandas/_libs/index.pyx", line 97, in pandas._libs.index.IndexEngine.get_loc  File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item  File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_itemKeyError: 5The above exception was the direct cause of the following exception:Traceback (most recent call last):  File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/Rise_Finder 2.py", line 29, in <module>    and not df_ticker.Close[i-1] >= df_ticker [i-2]:  File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 2899, in __getitem__    indexer = self.columns.get_loc(key)  File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2891, in get_loc    raise KeyError(key) from errKeyError: 5请帮忙,谢谢你的时间:)
查看完整描述

2 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

and not df_ticker.Close[i-1] >= df_ticker [i-2]:

KeyError: 5

df_ticker.Close[ 5 ] 或 df_ticker [ 5 ] 不存在。


查看完整回答
反对 回复 2023-06-06
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

它应该是:

df_ticker.Close[i] > df_ticker.Close[i-2]

我错过了第二次关闭。


查看完整回答
反对 回复 2023-06-06
  • 2 回答
  • 0 关注
  • 596 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信