Python:在多张表上将大 pandas DataFrame写入Excel的最快方式 [英] Python: fastest way to write pandas DataFrame to Excel on multiple sheets

查看:968
本文介绍了Python:在多张表上将大 pandas DataFrame写入Excel的最快方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将24只大熊猫数据框(140列×400行)导出到 Excel ,每个数据框都会以不同的方式导出。

I need to export 24 pandas data frames ( 140 columns x 400 rows) to Excel, each into a different sheet.

我正在使用大熊猫内置 ExcelWriter 。运行24个场景,需要:

I am using pandas’ built-in ExcelWriter. Running 24 scenarios, it takes:

51秒写入 .xls 文件(使用 xlwt

51 seconds to write to an .xls file (using xlwt)

86秒写入 .xlsx 文件(使用 XlsxWriter

86 seconds to write to an .xlsx file (using XlsxWriter)

141秒写入 .xlsm 文件(使用 openpyxl

141 seconds to write to an .xlsm file (using openpyxl)

21秒只运行程序(无Excel输出)

21 seconds to just run the program (no Excel output)

写入 .xls 的问题是电子表格不包含格式化样式,因此如果我在Excel中打开它,请选择一个列,然后单击逗号按钮格式化数字,它告诉我:没有找到样式的逗号。我没有写这个问题写到一个 .xlsx ,但是甚至更慢。

The problem with writing to .xls is that the spreadsheet contains no formatting styles, so if I open it in Excel, select a column, and click on the ‘comma’ button to format the numbers, it tells me: ‘style comma not found’. I don’t get this problem writing to an .xlsx, but that’s even slower.

有关如何使出口更快?
我不能成为第一个有这个问题的人,但经过几个小时的搜索论坛和网站,我还没有找到任何明确的解决方案。

Any suggestions on how to make the exporting faster? I can’t be the first one to have this problem, yet after hours of searching forums and websites I haven’t found any definite solution.

只有我想到的是使用 Python 导出到csv文件,然后编写一个Excel宏将所有CSV合并到一个电子表格中。

The only thing I can think of is to use Python to export to csv files, and then write an Excel macro to merge all the CSVs into a single spreadsheet.

.xls 文件是10 MB,而$ code> .xlsx 5.2 MB

The .xls file is 10 MB, and the .xlsx 5.2 MB

谢谢!

推荐答案

这是一个用于不同Python到Excel模块的基准测试

这里是140列x(400 x 24)的输出,在发布时使用最新版本的模块的行:

And here is the output for 140 columns x (400 x 24) rows using the latest version of the modules at the time of posting:

Versions:
    python      : 2.7.7
    openpyxl    : 2.0.5
    pyexcelerate: 0.6.3
    xlsxwriter  : 0.5.7
    xlwt        : 0.7.5

Dimensions:
    Rows = 9600 (400 x 24)
    Cols = 140

Times:
    pyexcelerate          :  11.85
    xlwt                  :  17.64
    xlsxwriter (optimised):  21.63
    xlsxwriter            :  26.76
    openpyxl   (optimised):  95.18
    openpyxl              : 119.29

与任何基准测试结果将依赖于Python /模块版本,CPU,RAM和磁盘I / O以及基准测试。所以请确保验证这些结果为您自己的设置。

As with any benchmark the results will depend on Python/module versions, CPU, RAM and Disk I/O and on the benchmark itself. So make sure to verify these results for your own setup.

另外,由于您特别询问熊猫,请注意,PyExcelerate 不支持

Also, since you asked specifically about Pandas, please note that PyExcelerate isn't supported.

这篇关于Python:在多张表上将大 pandas DataFrame写入Excel的最快方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆