拽猫的尾巴 发表于 2021-9-15 11:23:23

如何把用pandas生成的两份数据同时保存到一个excel表格里的两个sheet中


我生成了两份数据:df1,df2;
我想把这两份数据同时保存到一个excel表格里的两个sheet中,并把sheet名改成df1,df2。



import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.arange(24).reshape((6,4)), columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.arange(12).reshape((3,4)), columns = ['A','B','C','D'])


{:7_146:}

逃兵 发表于 2021-9-15 11:49:43


import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.arange(24).reshape((6,4)), columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.arange(12).reshape((3,4)), columns = ['A','B','C','D'])
path = 'test.xlsx'
writer = pd.ExcelWriter(path)
df1.to_excel(excel_writer = writer,sheet_name = 'df1')
df2.to_excel(excel_writer = writer,sheet_name = 'df2')
writer.save()
writer.close()
页: [1]
查看完整版本: 如何把用pandas生成的两份数据同时保存到一个excel表格里的两个sheet中