Pandas DataFrame 到 Excel 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31060885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas DataFrame to Excel Problems
提问by who_lee_oh
I'm trying to export a list of tuples to Excel via Pandas Datarame but every time i try to run the function, i get:
我正在尝试通过 Pandas Datarame 将元组列表导出到 Excel,但每次我尝试运行该函数时,我都会得到:
TypeError: init() got an unexpected keyword argument "engine"
TypeError: init() 得到了一个意外的关键字参数“engine”
The list of tuples is something like
元组列表类似于
[(83, 97), (34, 78), (39, 70), (60, 66), (90, 48)...]
Here is the code I'm using:
这是我正在使用的代码:
#Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(results, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
Does anyone know why this is occurring? Is this a case of a missing module? I'm working in an area that has no access to the internet so I can't download/update libraries. Could there be a different alternative to passing a list of tuples easily to an excel file?
有谁知道为什么会这样?这是缺少模块的情况吗?我在无法访问 Internet 的区域工作,因此无法下载/更新库。除了将元组列表轻松传递到 Excel 文件之外,还有其他替代方法吗?
回答by Ami Tavory
On my CentOS, I got your same exact problem. This was easily addressed with
在我的 CentOS 上,我遇到了与您完全相同的问题。这很容易解决
pip install xlsxwriter
(on your system you might have to do something a bit different; nevertheless, install this package).
(在您的系统上,您可能需要做一些不同的事情;不过,请安装此软件包)。
Following that, the problem changed to
之后,问题变成了
AttributeError: 'list' object has no attribute 'rfind'
However,
然而,
df.to_excel('data.xls', sheet_name='Sheet1', index=False, engine='xlsxwriter')
works.
作品。
回答by slushy
It depends on the version of openpyxl and pandas you have, but maybe this answerto this similar questionwill help. Just add the line:
这取决于openpyxl的版本和Pandas你有,但也许这个答案对这个类似的问题会有所帮助。只需添加以下行:
import pandas as pd
pd.core.format.header_style = None
when you import pandas, which deletes the formatting dictionary that is hard-coded in for the index row and header column in pandas.
当您导入 pandas 时,它会删除为 pandas 中的索引行和标题列硬编码的格式字典。
More detailed discussion is in this answertoo. Basically, openpyxl version 2 deprecated the style-conversion functions that map dictionary style definitions to the new openpyxl api, but Pandas still uses the old style, and for some reason the deprecation pass-through function errors out.
更详细的讨论也在这个答案中。基本上,openpyxl 版本 2 弃用了将字典样式定义映射到新openpyxl api的样式转换函数,但 Pandas 仍然使用旧样式,并且由于某种原因,弃用传递函数出错。
回答by Alexander
This works for me using Pandas 14.1.
这对我使用 Pandas 14.1 有效。
df = pd.DataFrame([(1, 2), (3, 4)])
>>> df
0 1
0 1 2
1 3 4
file_path = '~/Downloads/test.xlsx'
df.to_excel(file_path, index=False)
I don't believe there is a need to explicitly set the engine:
我不认为有必要明确设置引擎:
excel_writer : string or ExcelWriter object (File path or existing ExcelWriter)
excel_writer : 字符串或 ExcelWriter 对象(文件路径或现有 ExcelWriter)

