2016-09-26 216 views
4

我使用pd.read_html()從網頁導入表,但不是將數據結構化爲數據框,而是將Python作爲列表導入。如何將數據作爲數據框導入?謝謝!pd.read_html()導入列表而不是數據框

的代碼如下:

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 

回答

2

.read_html()產生dataframes的列表(有可能是在一個HTML源的多個表),得到由索引所需的一個。在你的情況下,有一個單一的數據框:

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

需要注意的是,如果沒有table S IN的HTML源代碼,它會返回一個錯誤,絕不會產生一個空列表。

+0

謝謝!確實df是一個數據框。但是,當我嘗試Python上的.head,.tail和.index參數時,Python返回了錯誤消息。我該如何解決這個問題? – AlK

+0

@AlexanderKonstantinidis有趣,'df.tail','df.head','df.index'適合我。你有什麼錯誤? – alecxe

+0

AttributeError:'DataFrame'對象沒有'heads'屬性,AttributeError:'DataFrame'對象沒有'tails'屬性,TypeError:'Index'對象不可調用 – AlK