Home > Software design >  read_html to dataframe, and not list of dataframes, then save to CSV
read_html to dataframe, and not list of dataframes, then save to CSV

Time:01-08

How come when i read the table of the homepage it is saved as a list of dataframes and not a single dataframe? when i look at the output of "dfhtml" it has 7 columns for each column in the table and a index spanning to the bottom of the table. i want to write the dataframe to a CSV but i cant because "AttributeError: 'list' object has no attribute 'to_csv'"

The reason here is its list of dataframes, i think so how do i make it in to one?

import pandas as pd


dfhtml = pd.read_html('https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list/')

dfhtml.to_csv('example',index=False)

CodePudding user response:

read_html returns a list for instances where a page has many tables on it.

If you just want the first table then do so like this.

dfhtml[0].to_csv('example',index=False)

Just need to be careful webpages are consistent and the desired table is always first etc

CodePudding user response:

pd.concat() will be helpful in your case.

Add the following line before converting to csv:

all_df = pd.concat(dfhtml)

Then, convert to csv.

all_df.to_csv('example.csv', index=False)
  •  Tags:  
  • Related