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创建了一个新环境。
在基本版本中,以上代码正常运行。 谁能解释为什么会这样吗?
答案 0 :(得分:0)
可能是因为,我使用source activate py36env
来激活创建的环境并为其安装了软件包(我使用的是conda 4.6.14)。
conda文档:
conda activate
和conda deactivate
仅适用于 conda 4.6和更高版本。对于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
现在可以正常工作