pandas,pd.ExcelWriter保存结果到已存在的excel文件中

背景:pandas支持将DataFrame数据直接保存到excel中
 
保存的case如下:
import pandas as pd
with pd.ExcelWriter('a.xls') as writer:
df1.to_excel(writer, 'sheet1')
df2.to_excel(writer, 'sheet2')
 
结果:新生成的结果sheet1和sheet2生成后,会将原有的excel表格中的数据全部覆盖
 
所以,在已有的excel文件上操作的方法如下:
from openpyxl import load_workbook
book = load_workbook('a.xlsx')
writer = pd.ExcelWriter('a.xlsx', engine='openpyxl')
writer.book = book
df1.to_excel(writer, 'sheet1')
df2.to_excel(writer, 'sheet2')
writer.save()
 
备注:
1、openpyxl为第三方包,安装方法:
pip3 install openpyxl
 
2、df1和df2为DataFrame格式数据,类似
df1 = pd.DataFrame({"需求名称": prd_name, "需求bug总数": prd_bugnum, "Android BUG数": prd_bugnum_android,
"iOS BUG数": prd_bugnum_ios, "server BUG数": prd_bugnum_server, "FE BUG数": prd_bugnum_fe,
"需求变更个数": prd_bugnum_pm, "设计缺陷个数": prd_bugnum_design
})
 
 
 
 
 
上一篇:No new migrations found. Your system is up-to-date.处理


下一篇:SQL Join 语句