【发布时间】:2017-12-21 07:59:47
【问题描述】:
我有一系列 10 个熊猫数据框,每个数据框有 100 行和 6 列。我正在尝试使用 openpyxl 将数据写入 xlsx。每个数据框都应位于工作簿的单独工作表中。正在创建工作表,但是,所有结果都只在第一张工作表上输入(所以我得到 10 个工作表,9 个空工作表和一个有 1000 行的工作表 - 当我应该有 10 个工作表,每个工作表有 100 行)。我怎样才能解决这个问题?
这是前两张纸的代码:
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)
【问题讨论】: