V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-07-29 10:59:46 +08:00 via iPhone · 2022 次点击
    这是一个创建于 1378 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    2 条回复    2020-07-29 16:32:27 +08:00
    renmu123
        1
    renmu123  
       2020-07-29 11:37:17 +08:00   ❤️ 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  
       2020-07-29 16:32:27 +08:00   ❤️ 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')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1070 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:19 · PVG 03:19 · LAX 12:19 · JFK 15:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.