OPENPYXL 填充新工作表
OPENPYXL populate new worksheets
我有一系列 10 个 pandas 数据框,每个数据框有 100 行和 6 列。我正在尝试使用 openpyxl 将数据写入 xlsx。每个数据框都应该在工作簿的单独工作 sheet 中。 sheet 正在创建,但是,所有结果仅在第一个 sheet 上输入(所以我得到 10 sheets,9 个为空,一个有 1000 行 - 当我应该有 10 sheets,每行 100 行)。我怎样才能解决这个问题?
这是前 2 sheets 的代码:
from openpyxl import Workbook
# Create the hospital_ranking workbook
hospital_ranking = Workbook()
dest_filename1 = "hospital_ranking.xlsx"
ws1 = hospital_ranking.active
ws1.title = "Nationwide"
from openpyxl.utils.dataframe import dataframe_to_rows
# Write the nationwide query to ws1
for r in dataframe_to_rows(national_results, index = False, header = True):
ws1.append(r)
for cell in ws1['A'] + ws1[1]:
cell.style = 'Pandas'
# Create the worksheet for each focus state
# CA
ws2 = hospital_ranking.create_sheet(title = 'California')
ws2 = hospital_ranking.active
# Write the CA query to ws2
for r in dataframe_to_rows(ca_results, index = False, header = True):
ws2.append(r)
for cell in ws2['A'] + ws2[1]:
cell.style = 'Pandas'
hospital_ranking.save(filename = os.path.join("staging/") + dest_filename1)
创建 sheet 后,您需要参考它:
不要将 ws2
重新绑定到工作簿的活动 sheet.
ws2 = hospital_ranking.active
等同于:
ws2 = ws1
您使事情过于复杂并且不需要您发布的大部分(如果不是全部)代码。只需使用接受 sheet_name
参数的 df.to_excel。
import pandas as pd
ew = pd.ExcelWriter('excel.xlsx')
list_of_dfs = [df1, df2, df3]
list_of_worksheet_names = [sheet1, sheet2, sheet3]
for df, sheet_name in zip(list_of_dfs, list_of_worksheet_names):
df.to_excel(ew, sheet_name=sheet_name)
ew.save()
对我们来说更简单的方法df.to_excel
# Create a Pandas Excel writer using openpyxl as the engine.
writer = pd.ExcelWriter(xlfile, engine='openpyxl')
# Write each dataframe to a different worksheet.
df1.to_excel(writer, sheet_name='California')
df2.to_excel(writer, sheet_name='Arizona')
df3.to_excel(writer, sheet_name='Texas')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
我有一系列 10 个 pandas 数据框,每个数据框有 100 行和 6 列。我正在尝试使用 openpyxl 将数据写入 xlsx。每个数据框都应该在工作簿的单独工作 sheet 中。 sheet 正在创建,但是,所有结果仅在第一个 sheet 上输入(所以我得到 10 sheets,9 个为空,一个有 1000 行 - 当我应该有 10 sheets,每行 100 行)。我怎样才能解决这个问题?
这是前 2 sheets 的代码:
from openpyxl import Workbook
# Create the hospital_ranking workbook
hospital_ranking = Workbook()
dest_filename1 = "hospital_ranking.xlsx"
ws1 = hospital_ranking.active
ws1.title = "Nationwide"
from openpyxl.utils.dataframe import dataframe_to_rows
# Write the nationwide query to ws1
for r in dataframe_to_rows(national_results, index = False, header = True):
ws1.append(r)
for cell in ws1['A'] + ws1[1]:
cell.style = 'Pandas'
# Create the worksheet for each focus state
# CA
ws2 = hospital_ranking.create_sheet(title = 'California')
ws2 = hospital_ranking.active
# Write the CA query to ws2
for r in dataframe_to_rows(ca_results, index = False, header = True):
ws2.append(r)
for cell in ws2['A'] + ws2[1]:
cell.style = 'Pandas'
hospital_ranking.save(filename = os.path.join("staging/") + dest_filename1)
创建 sheet 后,您需要参考它:
不要将 ws2
重新绑定到工作簿的活动 sheet.
ws2 = hospital_ranking.active
等同于:
ws2 = ws1
您使事情过于复杂并且不需要您发布的大部分(如果不是全部)代码。只需使用接受 sheet_name
参数的 df.to_excel。
import pandas as pd
ew = pd.ExcelWriter('excel.xlsx')
list_of_dfs = [df1, df2, df3]
list_of_worksheet_names = [sheet1, sheet2, sheet3]
for df, sheet_name in zip(list_of_dfs, list_of_worksheet_names):
df.to_excel(ew, sheet_name=sheet_name)
ew.save()
对我们来说更简单的方法df.to_excel
# Create a Pandas Excel writer using openpyxl as the engine.
writer = pd.ExcelWriter(xlfile, engine='openpyxl')
# Write each dataframe to a different worksheet.
df1.to_excel(writer, sheet_name='California')
df2.to_excel(writer, sheet_name='Arizona')
df3.to_excel(writer, sheet_name='Texas')
# Close the Pandas Excel writer and output the Excel file.
writer.save()