Fork me on GitHub

将DataFrame写入同个表的不同sheetname

将DataFrame写入同个表格的不同sheetname

在实际工作中总会遇到这样的需求:将类型的数据放在一个excel表格中,但是位置在不同的sheetname。本文介绍使用pandas来实现这样的需求。

方法

通过pandas的ExcelWriter方法来实现,比如现在有3个不同的DataFrame,我们通过如下的代码来实现数据写入:

  • 实例化一个ExcelWriter对象
  • 通过对象的to_excel方法来分批写入
1
2
3
4
5
6
7
8
9
10
import pandas as px

# 1、准备好3个DataFrame

# 2、写入数据
writer = pd.ExcelWriter("学生成绩.xlsx") # 设置表名

df1.to_excel(writer,"语文",index=False) # 第一个sheetname,同时去掉DataFrame中的行索引
df2.to_excel(writer,"数学",index=False)
df3.to_excel(writer,"英语",index=False)

本文标题:将DataFrame写入同个表的不同sheetname

发布时间:2021年06月03日 - 17:06

原始链接:http://www.renpeter.cn/2021/06/03/%E5%B0%86DataFrame%E5%86%99%E5%85%A5%E5%90%8C%E4%B8%AA%E8%A1%A8%E7%9A%84%E4%B8%8D%E5%90%8Csheetname.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea