推荐学习书目
› 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
demonps
V2EX  ›  Python

请教个 pandas 问题, excel 某一列是包含货币样式的公式,pandas.read_excel 得到的结果全是 0...

  •  
  •   demonps · Dec 25, 2023 · 3525 views
    This topic created in 1007 days ago, the information mentioned may be changed or developed.

    写入如下:

    with pd.ExcelWriter(file_name, engine='xlsxwriter') as writer:
        fmt = writer.book.add_format({'num_format': '¥#,##0.00;[红色]¥ -#,##0.00'})
        df.to_excel(writer, sheet_name='xxx', index=False)
        writer.sheets['stats'].set_column('K:K', None, fmt)
    

    读取就是简单的 pd.read_excel(...)

    2 replies  •  2023-12-25 20:50:57 +08:00
    NoOneNoBody
        1
    NoOneNoBody  
       Dec 25, 2023
    前置:不熟悉 excel ,可能楼下其他答案更佳

    write 使用特定 engine 时,read 也要指定 engine ,而且你这个金额都是 str ,也要考虑 dtype 问题,thousands 参数好象是做这事的,但可能更有效是这个 engine 相关的参数 engine_kwargs
    demonps
        2
    demonps  
    OP
       Dec 25, 2023
    感谢回复
    金额列长这样
    =K2*G2/100/100
    =((H2-G2)*L2+(I2-G2)*M2+(J2-G2)*N2) / 100 / 100

    给他设置了货币格式之后读到的就全是 0 了~~,试过设置 dtype,不太行. engine 因为 xlsxwriter 是个写 excel 的 lib 不支持读的

    我去看看那个 thousands
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   849 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:14 · PVG 03:14 · LAX 12:14 · JFK 15:14
    ♥ Do have faith in what you're doing.