• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

pandas分别写入excel的不同sheet方法

python 搞代码 4年前 (2022-01-07) 103次浏览 已收录 0个评论

今天小编就为大家分享一篇pandas分别写入excel的不同sheet方法,具有很好来源gaodai#ma#com搞*!代#%^码$网的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

pandas可以非常方便的写数据到excel,那么如何写多个dataframe到不同的sheet呢?

使用pandas.ExcelWriter

 import pandas as pd writer = pd.ExcelFile('your_path.xlsx') df1 = pd.DataFrame() df2 = pd.DataFrame() df1.to_excel(writer, sheet_name='df_1') df2.to_excel(writer, sheet_name='df_2') writer.save()

网上的大部分答案基本上都是这些内容,但是这里有个大坑,你会发现找不到想要的xlsx文件。

那么问题出在哪?

我们看看ExcelWriter源码就知道了

 class ExcelFile(object): """ Class for parsing tabular excel sheets into DataFrame objects. Uses xlrd. See read_excel for more documentation Parameters ---------- io : string, path object (pathlib.Path or py._path.local.LocalPath), file-like object or xlrd workbook If a string or path object, expected to be a path to xls or xlsx file engine: string, default None If io is not a buffer or path, this must be set to identify io. Acceptable values are None or xlrd """

这里已经说的很清楚了,希望传入的是excel的路径,你只传了个文件名,当然找不到了。

而且从这里我们可以看到,pandas.ExcelWriter实质上是用xlrd来解析excel的。这个wrapper提供了更简单的接口。

以上这篇pandas分别写入excel的不同sheet方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是pandas分别写入excel的不同sheet方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:pandas分别写入excel的不同sheet方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址