Pandas读取HTML表格

6
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)

以下是错误信息的结果。当我打印硬币时,我得到完整的表格,但是当我试图获取特定的信息时,它会给出这个错误消息。我知道这个格式是有效的,因为我已经从我学习的其他练习中粘贴了它,并且只是更改了网站。非常感谢。
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

非常感谢,我完全明白你的意思,谢谢。 - top bantz
1个回答

2
如果 df 是一个数据帧,则像 df[column] 这样的索引将寻找名为 column。在您的情况下,coins[0] 是一个数据帧,它没有一列叫做 1。但是,它确实有一列叫做 Name,因此要打印所有名称,请执行以下操作:
df = coins[0]
for name in df['Name']:
    print(name)

您也可以使用 df['Name'].unique() 仅打印唯一的名称。 - joaovictortr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接