如何将多个 pandas 数据帧写入单个输出Excel文件? [英] How to write multiple pandas data frames to single output excel file?

查看:32
本文介绍了如何将多个 pandas 数据帧写入单个输出Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Python3,并且具有大量相关的熊猫数据框.我需要将每个数据框作为单独的选项卡将其写入单个excel文件,我可以将它们分别单独写入excel文件,然后将其复制并粘贴到单个文件中.但是,我正在寻找一种更自动化的解决方案.预先感谢您的帮助.

I am currently working on Python3 and have a large number of related pandas dataframes. I need to write it to single excel file with each dataframe as a separate tab, I can write all of them separately to excel files and then copy, paste them to a single file. However, I am looking for a more automated solution to the problem. Thanks in advance for the help.

推荐答案

以下代码可为您完成此操作:

Here is code that does that for you:

fname = 'foobar.xlsx'
writer = pd.ExcelWriter(fname)
df1.to_excel(writer, "sheet1", index=False)
df2.to_excel(writer, "sheet2", index=False)
## more dataframes goes here
writer.save()

这篇关于如何将多个 pandas 数据帧写入单个输出Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆