pd.read_html()导入列表而不是数据框 [英] pd.read_html() imports a list rather than a dataframe

查看:691
本文介绍了pd.read_html()导入列表而不是数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pd.read_html()从网页中导入表,但没有将数据构造为数据框,而是将Python作为列表导入.如何将数据作为数据框导入?谢谢!

I used pd.read_html() to import a table from a webpage but instead of structuring the data as a dataframe Python imported it as a list. How can I import the data as a dataframe? Thank you!

代码如下:

import pandas as pd

import html5lib

url = 'http://www.fdic.gov/bank/individual/failed/banklist.html'

dfs = pd.read_html(url)

type(dfs)

Out[1]: list

推荐答案

.read_html() produces a list of dataframes (there could be multiple tables in an HTML source), get the desired one by index. In your case, there is a single dataframe:

dfs = pd.read_html(url)
df = dfs[0]
print(df)

请注意,如果HTML源代码中没有table,它将返回一个错误,并且永远不会产生一个空列表.

Note that, if there are no tables in the HTML source, it would return an error and would never produce an empty list.

这篇关于pd.read_html()导入列表而不是数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆