解决pandas .to_excel不覆盖已有sheet的问题

yipeiwu_com6年前Python基础

直接to_excel会被覆盖,借助ExcelWriter可以实现写多个sheet。

from openpyxl import load_workbook
excelWriter = pd.ExcelWriter(os.path.join(output_dir, 'datapoint_statistic.xlsx'),
            engine='openpyxl')
pd.DataFrame().to_excel(os.path.join( output_dir,'datapoint_statistic.xlsx'))
#excel必需已经存在,因此先建立一个空的sheet
_excelAddSheet(DataFrame, excelWriter, sheet_name)
 
def _excelAddSheet(self,dataframe,excelWriter,sheet_name):
  book = load_workbook(excelWriter.path)
  excelWriter.book = book
  dataframe.to_excel(excel_writer=excelWriter,sheet_name=sheet_name,index=None)
  excelWriter.close()

以上这篇解决pandas .to_excel不覆盖已有sheet的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

基于python内置函数与匿名函数详解

基于python内置函数与匿名函数详解

内置函数 Built-in Functions abs() dict() help()...

Pytorch 实现自定义参数层的例子

注意,一般官方接口都带有可导功能,如果你实现的层不具有可导功能,就需要自己实现梯度的反向传递。 官方Linear层: class Linear(Module): def __in...

Python静态类型检查新工具之pyright 使用指南

Python静态类型检查新工具之pyright 使用指南

Python是一门动态类型的语言,民间流传一种说法叫”动态一时爽,重构火葬场”,听起来够吓人的,好在这门语言在不断地改进,包括对 PEP484 引入的类型提示(Type Hint),就是...

matplotlib绘制多个子图(subplot)的方法

在matplotlib下,一个Figure对象可以包含多个子图(Axes),可以使用subplot()快速绘制,其调用形式如下: subplot(numRows, numCols,...

go和python调用其它程序并得到程序输出

go和python调用其它程序并得到程序输出

在c语言中可以用system函数调用系统命令并得到输出,通过输出重定向也可以将程序执行的输出保存到文件以供使用,但用起来不是很方便。我这里介绍下用python和go语言的实现方式,可以将...