Pandas 写入 Excel 的几种情形与方式,覆盖,新增,追加,对齐
import numpy as np
import pandas as pd
覆盖所有原有数据,只保留最后一份数据:
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
s1.to_excel('test.xlsx', sheet_name="111", index=False)
s2.to_excel('test.xlsx', sheet_name="222", index=False)
覆盖所有原有数据,保留当前写入的多份数据:
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
with pd.ExcelWriter("test.xlsx") as writer:
s1.to_excel(writer, sheet_name="111", index=False)
s2.to_excel(writer, sheet_name="222", index=False)
保留原有数据,新开 一个sheet 写入数据
from openpyxl import load_workbook
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
s1.to_excel('test.xlsx', sheet_name='111', index=False)
book = load_workbook("test.xlsx")
with pd.ExcelWriter("test.xlsx") as writer:
writer.book = book
s2.to_excel(writer, sheet_name="222", index=False)
重写 指定sheet数据,保留原有的其余sheet数据
from openpyxl import load_workbook
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
s3 = pd.DataFrame(np.array([['s3', 's3', 's3', 's3']]), columns=['a', 'b', 'c', 'd'])
with pd.ExcelWriter("test.xlsx") as writer:
s1.to_excel(writer, sheet_name="111", index=False)
s2.to_excel(writer, sheet_name="222", index=False)
book = load_workbook("test.xlsx")
with pd.ExcelWriter("test.xlsx") as writer:
writer.book = book
writer.sheets = {i.title: i for i in book.worksheets}
s3.to_excel(writer, sheet_name="111", index=False)
修改指定sheet内的部分数据,其余保持不变
from openpyxl import load_workbook
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
with pd.ExcelWriter("test.xlsx") as writer:
s1.to_excel(writer, sheet_name="111", index=False)
s2.to_excel(writer, sheet_name="222", index=False)
book = load_workbook("test.xlsx")
with pd.ExcelWriter("test.xlsx") as writer:
writer.book = book
sheet = book['222']
sheet.cell(2, 1, 'hello')
sheet['b2'] = '你好'
向 sheet 中追加数据
1.在Excel 中追加
from openpyxl import load_workbook
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
s4 = pd.DataFrame(np.array([['s4b', 's4d', 's4c']]), columns=['b', 'd', 'c'])
with pd.ExcelWriter("test.xlsx") as writer:
s1.to_excel(writer, sheet_name="111", index=False)
s2.to_excel(writer, sheet_name="222", index=False)
df = pd.read_excel('test.xlsx', sheet_name='111')
row = df.shape[0]
s4 = pd.concat([pd.DataFrame(columns=df.columns), s4], ignore_index=True)
book = load_workbook("test.xlsx")
with pd.ExcelWriter("test.xlsx") as writer:
writer.book = book
writer.sheets = {sheet.title: sheet for sheet in book.worksheets}
s4.to_excel(writer, sheet_name='111', startrow=row + 1, index=False, header=False)
向sheet中追加数据
2.在pandas中追加数据后,重写指定sheet
from openpyxl import load_workbook
s1 = pd.DataFrame(np.array([['s1', 's1', 's1', 's1']]), columns=['a', 'b', 'c', 'd'])
s2 = pd.DataFrame(np.array([['s2', 's2', 's2', 's2']]), columns=['a', 'b', 'c', 'd'])
s4 = pd.DataFrame(np.array([['s4b', 's4d', 's4c']]), columns=['b', 'd', 'c'])
with pd.ExcelWriter("test.xlsx") as writer:
s1.to_excel(writer, sheet_name="111", index=False)
s2.to_excel(writer, sheet_name="222", index=False)
df = pd.read_excel('test.xlsx', sheet_name='111')
df = pd.concat([df, s4], ignore_index=True)
book = load_workbook("test.xlsx")
with pd.ExcelWriter("test.xlsx") as writer:
writer.book = book
writer.sheets = {sheet.title: sheet for sheet in book.worksheets}
df.to_excel(writer, sheet_name='111', index=False)
|