Skip to content
Advertisement

How do I export a read_html df to Excel, when it related to table ID rather than data in the code?

I am experiencing this error with the code below:

JavaScript

I want to save the table I am scraping from wikipedia to an Excel file – but I can’t work out how to adjust the code to get the data list from the terminal to the Excel file using to_excel.

I can see it works for a similar problem when a dataset has data set out as a ‘DataFrame’ (i.e. df = pd.DataFrame(data, columns = ['Product', 'Price']).

But can’t work out how to adjust my code for the df = pd.read*html(str(congresstable)) line – which I think is the issue. (i.e. using read*_html and sourcing the data from a table id)

How can I adjust the code to make it save an excel file to the path specified?

JavaScript

I was expecting the data list to be saved to Excel at the folder path specified.

I tried following multiple guides, but they don’t show the read_html item, only DataFrame solutions.

Advertisement

Answer

pandas.read_html() creates a list of tables respectiv dataframe objects, so you have to pick one by index in your case [0] – You also do not need requests and BeautifulSoup, separatly, just go with pandas.read_html()

JavaScript

Example

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