推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wittyfans
V2EX  ›  Python

如何给 pd.to_excel 加进度条?

  •  
  •   wittyfans · Jul 29, 2020 via iPhone · 2543 views
    This topic created in 2141 days ago, the information mentioned may be changed or developed.

    需求是将多个文件分别或合并处理,再按需求导出到 excel 的不同 sheet 。计算过程还好,速度挺快的,但导出 Excel 的时候就很慢,想加个进度条,有了解的 v 友吗?

    目前是用 pd.ExcelWriter 将文件导出。尝试了 tqdm,但好像只支持对 pandas 计算过程直接上进度条,试了一下 Dask 的 ProgresBar 好像也不行。

    2 replies    2020-07-29 16:32:27 +08:00
    renmu123
        1
    renmu123  
       Jul 29, 2020   ❤️ 2
    pd.to_excel 依赖的是第三方的 excel 读写库,你可以试试看将 Dataframe 手动变成 python 的数据结构后使用 openpyxl 或者 xlsxwriter 导出至 excel,速度可能会比直接使用 pandas 快一点(我也没有经过测试)

    PS:如果使用 openpyxl 的话,可以使用 write_only 模式
    PPS:我最近在翻译 openpyxl 的中文文档,欢迎围观(第一遍还没翻译完) https://openpyxl-chinese-docs.readthedocs.io/zh_CN/latest/optimized.html
    wuwukai007
        2
    wuwukai007  
       Jul 29, 2020   ❤️ 1
    from copy import deepcopy
    from tqdm import tqdm
    from openpyxl.worksheet import Worksheet
    cell_base = deepcopy(Worksheet.cell)
    def cell(*args,**kwargs):
    ---- _total.update(1)
    ----return cell_base(*args,**kwargs)
    Worksheet.cell = cell

    df = pd.read_sql(xxxx)
    excel_len = (len(df)+1) * (len(df.columns)+1)
    _total = tqdm(range(excel_len),desc='table_name')
    df.to_excel('a.xlsx')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3385 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:11 · PVG 20:11 · LAX 05:11 · JFK 08:11
    ♥ Do have faith in what you're doing.