Skip to content
Advertisement

Get HTML table into pandas Dataframe, not list of dataframe objects

I apologize if this question has been answered elsewhere but I have been unsuccessful in finding a satisfactory answer here or elsewhere.

I am somewhat new to python and pandas and having some difficulty getting HTML data into a pandas dataframe. In the pandas documentation it says .read_html() returns a list of dataframe objects, so when I try to do some data manipulation to get rid of the some samples I get an error.

Here is my code to read the HTML:

JavaScript

Then I try to clean it up:

JavaScript

And I received the following error:

JavaScript

How do I get this data into an actual dataframe, similar to what .read_csv() does?

Advertisement

Answer

From https://pandas.pydata.org/pandas-docs/version/0.17.1/io.html#io-read-html, read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content”.

So df = df[0].dropna(axis=0, thresh=4) should do what you want.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement