I am attempting to save data from multiple tables brought in through pd.read_html()
. If I print df
, I can see it captured all the data, but when saving the data it is only saving the first table to excel. How do I separate out the tables so I can save each one to a separate sheet in excel (i.e. Quarterly Income Statement on sheet1, Annual Income Statement on sheet2, etc.). Below is my code. Any help is appreciated.
我试图通过pd.read_html()从多个表中保存数据。如果我打印df,我可以看到它捕获了所有数据,但是在保存数据时,它只将第一个表保存为excel。如何将表格分开,以便将每个表格保存到Excel中的单独表格中(即表1中的季度损益表,表2中的年度损益表等)。以下是我的代码。任何帮助表示赞赏。
dfs = pd.read_html(https://www.google.com/finance?q=googl&fstype=ii, flavor='html5lib')
writer = pd.ExcelWriter(output.xlsx, engine='xlsxwriter')
for df in dfs:
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
1 个解决方案
#1
7
You can iterate on your list and flush them to a new sheet of the same workbook
您可以迭代列表并将它们刷新到同一工作簿的新工作表
import pandas as pd
dfs = pd.read_html('https://www.google.com/finance?q=googl&fstype=ii', flavor='html5lib')
# Create a Pandas Excel writer.
xlWriter = pd.ExcelWriter('myworkbook.xlsx', engine='xlsxwriter')
# Write each df to its own sheet
for i, df in enumerate(dfs):
df.to_excel(xlWriter, sheet_name='Sheet{}'.format(i))
# Close the writer and output the Excel file (mandatory!)
xlWriter.save()
#1
7
You can iterate on your list and flush them to a new sheet of the same workbook
您可以迭代列表并将它们刷新到同一工作簿的新工作表
import pandas as pd
dfs = pd.read_html('https://www.google.com/finance?q=googl&fstype=ii', flavor='html5lib')
# Create a Pandas Excel writer.
xlWriter = pd.ExcelWriter('myworkbook.xlsx', engine='xlsxwriter')
# Write each df to its own sheet
for i, df in enumerate(dfs):
df.to_excel(xlWriter, sheet_name='Sheet{}'.format(i))
# Close the writer and output the Excel file (mandatory!)
xlWriter.save()