pandas可以非常方便的写数据到excel,那么如何写多个data 到不同的sheet呢?
使用pandas.ExcelWriter
import pandas as pd writer = pd.ExcelFile(\'your_path.xlsx\') df1 = pd.Data () df2 = pd.Data () df1.to_excel(writer, sheet_name=\'df_1\') df2.to_excel(writer, sheet_name=\'df_2\') writer.save()
网上的大部分答案基本上都是这些内容,但是这里有个大坑,你会发现找不到想要的xlsx文件。
那么问题出在哪?
我们看看ExcelWriter源码就知道了
class ExcelFile( ):
\"\"\"
Class for parsing tabular excel sheets into Data s.
Uses xlrd. See read_excel for more documentation
Parameters
----------
io : string, path (pathlib.Path or py._path.local.LocalPath),
file-like or xlrd workbook
If a string or path , 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方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
解密区块链(九):区块扩容折中方案
下一篇 :
Cgroup原理解释及部署实例(3)
-
关闭Windows休眠功能,删除 hiberfil.sys 文件,腾出可用空间 - Windows 10
2026-05-19栏目: 教程
-
快照技术使用
2026-05-19栏目: 教程
-
受用一生的高效 PyCharm 使用技巧(六)
2026-05-19栏目: 教程
-
用户数从 0 到亿,我的 K8s 踩坑血泪史
2026-05-19栏目: 教程
-
PgSQL · 特性分析 · 浅析PostgreSQL 中的JIT
2026-05-19栏目: 教程
