【发布时间】:2019-07-24 00:38:32
【问题描述】:
我是 Python 新手(和一般编程),在将数据写入 Excel 中的工作表时遇到了问题。
我正在读取 Excel 文件,对特定列执行总和计算,然后将结果写入新工作簿。最后,它会根据结果创建两个图表。
代码有效,除了每次我运行它时,它都会创建新的工作表,并在末尾附加数字。我真的只是希望它覆盖我提供的工作表名称,而不是创建新的。
我对所有模块不够熟悉,无法理解所有可用选项。我研究了 openpyxl 和 pandas,以及我正在尝试做的类似示例要么不容易找到,要么在我尝试时似乎不起作用。
import pandas as pd
import xlrd
import openpyxl as op
from openpyxl import load_workbook
import matplotlib.pyplot as plt
# declare the input file
input_file = 'TestData.xlsx'
# declare the output_file name to be written to
output_file = 'TestData_Output.xlsx'
book = load_workbook(output_file)
writer = pd.ExcelWriter(output_file, engine='openpyxl')
writer.book = book
# read the source Excel file and calculate sums
excel_file = pd.read_excel(input_file)
num_events_main = excel_file.groupby(['Column1']).sum()
num_events_type = excel_file.groupby(['Column2']).sum()
# create dataframes and write names and sums out to new workbook/sheets
df_1 = pd.DataFrame(num_events_main)
df_2 = pd.DataFrame(num_events_type)
df_1.to_excel(writer, sheet_name = 'TestSheet1')
df_2.to_excel(writer, sheet_name = 'TestSheet2')
# save and close
writer.save()
writer.close()
# dataframe for the first sheet
df = pd.read_excel(output_file, sheet_name='TestSheet1')
values = df[['Column1', 'Column3']]
# dataframe for the second sheet
df = pd.read_excel(output_file, sheet_name='TestSheet2')
values_2 = df[['Column2', 'Column3']]
# create the graphs
events_graph = values.plot.bar(x = 'Column1', y = 'Column3', rot = 60) # rot = rotation
type_graph = values_2.plot.bar(x = 'Column2', y = 'Column3', rot = 60) # rot = rotation
plt.show()
我得到了预期的结果,并且图表工作正常。我真的很想在每次运行时覆盖工作表。
【问题讨论】:
-
不使用
load_workbook怎么办?据我回忆,pd.ExcelWriter在初始化时会覆盖现有工作簿。 -
writer = pd.ExcelWriter(output_file, engine='openpyxl')已经有一本书了,所以没有理由使用book = load_workbook(output_file) -
dubbbdan,谢谢!那行得通!感谢您的帮助。
-
在您的帖子中发布了答案。请接受并投票(如果您认为值得)。
标签: python