我的DataFrame像x = 1,2,5,6一样处于循环状态
dfx
Dataframe : df1,df2,df5,df6
文件路径;
file1 = 'G:\\python\\out\\consumption_rice_1.xlsx'
file2 = 'G:\\python\\out\\consumption_rice_2.xlsx'
file5 = 'G:\\python\\out\\consumption_rice_5.xlsx'
file6 = 'G:\\python\\out\\consumption_rice_6.xlsx'
想将df1导出到file1,...,df6导出到file6。
请帮助我如何使用循环导出。
答案 0 :(得分:1)
循环后,您可以导出到一个包含多张工作表的excel文件。对于Ex-
writer = pd.ExcelWriter('consumption_rice.xlsx')
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2')
df3.to_excel(writer,'Sheet3')
df4.to_excel(writer,'Sheet4')
writer.save()
演示: https://xlsxwriter.readthedocs.io/example_pandas_multiple.html
如果您仍然需要多个.xlsx
,请尝试循环执行(虽然未测试)
df_list = [df1,df2,df3,df4]
for key, df in df_list:
writer = pd.ExcelWriter('consumption_rice_'+key+'.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()