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
demonps
V2EX  ›  Python

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

  •  
  •   demonps · 124 天前 · 876 次点击
    这是一个创建于 124 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写入如下:

    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 条回复    2023-12-25 20:50:57 +08:00
    NoOneNoBody
        1
    NoOneNoBody  
       124 天前
    前置:不熟悉 excel ,可能楼下其他答案更佳

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

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

    我去看看那个 thousands
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4391 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:10 · PVG 18:10 · LAX 03:10 · JFK 06:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.