python - Pandas 在使用 python 3.6 创建新环境后给出 KeyError:0
问题描述
df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0][0][1:]
给我以下错误
Traceback (most recent call last):
File "/Users/shijith/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2657, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "delete.py", line 76, in <module>
stock_Symbols = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0][0]
File "/Users/shijith/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/shijith/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
工作正常,再次尝试切片时出错。
我正在使用 anaconda 并在我的基本环境中安装了 python 3.7。但由于我必须使用 H20,我用 python 3.6 创建了一个新环境。
在基本版本中,上述代码可以正常工作。谁能解释为什么会这样?
解决方案
可能是因为,我曾经source activate py36env
激活创建的环境并将软件包安装到它(我使用的是 conda 4.6.14)。
根据 conda docs 激活创建的环境使用:
对于 conda 4.6 及更高版本,请使用。
conda activate
和conda deactivate
对于 4.6 之前的 conda 版本,请使用:
Windows:activate
和deactivate
Linux 和 macOS:source activate
和source deactivate
我不得不删除该环境并再次创建(conda create -n envname python=3.5 pandas scikit-learn
),使用激活conda activate envname
现在它的工作
推荐阅读
- angular - 带有primeng 7或Angular 2+的angular-fontawesome 5图标
- sql-server - 如何根据表 2 中的共享订单 ID 并受日期参数限制从表 1 中返回唯一 ID 的计数
- angular - 更新的代码在部署 Angular 4 后没有反映
- python - 从 MNIST 数据集中更改训练集和测试集的大小
- javascript - 有没有办法连接 req.body 请求?
- angular-cli - Angular `ng serve --prod` 和 `ng serve -c production` 一样吗?
- r - 如何使用查找表替换 data.table 列中的值?[R]
- symfony - 查询执行后 Apollo 客户端 + graphql + api-platform 错误
- java - 使用 AngularJS 和 Spring Boot 的 Websocket
- image-processing - 链码如何保持缩放不变?