当前位置: 移动技术网 > IT编程>脚本编程>Python > pandas分别写入excel的不同sheet方法

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

2019年01月09日  | 移动技术网IT编程  | 我要评论

quantity是什么意思,三线人家,必途网

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方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网