【问题标题】:AttributeError: 'list' object has no attribute 'head'AttributeError:“列表”对象没有属性“头”
【发布时间】:2022-01-23 05:54:57
【问题描述】:
import pandas as pd
df = pd.read_html("https://github.com/KeithGalli/pandas/blob/master/pokemon_data.csv")
print (df)

它工作正常,然后当我尝试对文件进行操作时,它只会给我以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-24-eb4b6e6a1e41> in <module>()
----> 1 print (df.head)

AttributeError: 'list' object has no attribute 'head'

【问题讨论】:

  • pd.read_html 返回数据帧列表,而不是数据帧。使用df[0].head() 查看第一个表。

标签: pandas google-colaboratory


【解决方案1】:

pd.read_html 尝试从 HTML 文件中读取表格,但您正在尝试读取 CSV 文件,因此您想改用 pd.read_csv

另外,您使用的网址无效。您想将actual URL 转换为原始 CSV 文件:

import pandas as pd
df = pd.read_csv("https://github.com/KeithGalli/pandas/blob/master/pokemon_data.csv")
print (df)

【讨论】:

  • 该 URL 应该是 https://raw.githubusercontent.com/KeithGalli/pandas/master/pokemon_data.csv。另一个 URL 将为他们提供 CSV 文件的 HTML 预览。
  • 好点,这可能是 OP 一开始就出错了。
  • 非常感谢,尼克,它似乎可以正常工作,实际上我之前不得不玩过它,我最终决定尝试使用 html,但多亏了你,它似乎可以正常工作了跨度>
  • @Myamine 如果这解决了您的问题,请查看this page,因为您是新来的 ;)
猜你喜欢
  • 2021-07-05
  • 2018-01-16
  • 2016-05-14
  • 2016-12-21
  • 2022-01-23
  • 2019-11-01
  • 2021-08-23
  • 2016-04-22
  • 2019-12-28
相关资源
最近更新 更多