谢谢。我相信,一个完整的示例可能会对遇到相同问题的其他人有所帮助:
import pandas as pdimport numpy as nppath = r"C:UsersfedelDesktopexcelDataPhD_data.xlsx"x1 = np.random.randn(100, 2)df1 = pd.Dataframe(x1)x2 = np.random.randn(100, 2)df2 = pd.Dataframe(x2)writer = pd.ExcelWriter(path, engine = 'xlsxwriter')df1.to_excel(writer, sheet_name = 'x1')df2.to_excel(writer, sheet_name = 'x2')writer.save()writer.close()
根据我的理解,在这里我生成一个excel文件,无论它是通过“ xslxwriter”还是“ openpyxl”引擎生成的,都没有关系。
当我想写而不丢失原始数据时
import pandas as pdimport numpy as npfrom openpyxl import load_workbookpath = r"C:UsersfedelDesktopexcelDataPhD_data.xlsx"book = load_workbook(path)writer = pd.ExcelWriter(path, engine = 'openpyxl')writer.book = bookx3 = np.random.randn(100, 2)df3 = pd.Dataframe(x3)x4 = np.random.randn(100, 2)df4 = pd.Dataframe(x4)df3.to_excel(writer, sheet_name = 'x3')df4.to_excel(writer, sheet_name = 'x4')writer.save()writer.close()
此代码可以完成工作!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)