首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >熊猫阅读html表格

问熊猫阅读html表格
EN

Stack Overflow用户
提问于 2018-09-02 15:19:10
回答 1查看 1.9K关注 0票数 6
代码语言:javascript
复制
import pandas as pd
import pandas_datareader.data as web

coins = pd.read_html('https://coinmarketcap.com/')

for name in coins[0][1][1:]:
    print(name)

结果将出现以下错误消息。当我打印硬币时,我得到了完整的表格,但是当我试图获取特定的信息时,它会给我这个错误信息。我知道这一格式的工作,因为我已经复制了它完全从其他练习,我一直在学习,并刚刚改变了网站。非常感谢。

代码语言:javascript
复制
C:\Users\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Desktop/python_work/crypto/crypto_corr.py
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 2525, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Desktop/python_work/crypto/crypto_corr.py", line 6, in <module>
    for name in coins[0][1][1:]:
  File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\internals.py", line 3843, in get
    loc = self.items.get_loc(item)
  File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1

Process finished with exit code 1
EN

回答 1

Stack Overflow用户

发布于 2018-09-02 16:35:40

如果df是一个数据格式,那么像df[column]这样的索引会查找列(称为column )。在您的示例中,coins[0]是一个dataframe,它没有列1。但是,它有一个列Name,因此要打印所有名称,请执行以下操作:

代码语言:javascript
复制
df = coins[0]
for name in df['Name']:
    print(name)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52138325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档