gpt4 book ai didi

excel - Pandas to_excel( ) 输出 float 不对

转载 作者:行者123 更新时间:2023-12-04 22:12:09 25 4
gpt4 key购买 nike

我有一些奇怪的情况试图在 to_excel pandas 函数中获取输出。
我尝试读取编号为“21631706.9893399”的 xlsx excel 文件,但是当尝试写入新的 xlsx excel 文件时,我会得到输出“21631706.98934”。我使用 openpyxl 和 xlsxwriter 进行了测试,但得到了相同的结果,但是如果我尝试使用 xlwt 引擎编写 xls excel,它给了我正确的答案,有没有办法使用这些引擎读取和写入相同的浮点数?我怀疑 xlsx 文件中存在一些浮点限制,但在 xlsx 输入中,数字很好。
我的代码:

import pandas as pd

df = pd.read_excel(r'input\sample.xlsx')

df.to_excel(r'output\excel xlsx - xlsxwriter.xlsx', engine='xlsxwriter', index=False)
df.to_excel(r'output\excel xlsx - openpyxl.xlsx', engine='openpyxl', index=False)
df.to_excel(r'output\excel xls - xlwt.xls', engine='xlwt', index=False)
Github repository with the samples
依赖项:
python = "3.7.4"
pandas = "1.3.5"
XlsxWriter = "3.0.3"
openpyxl = "3.0.9"
xlwt = "1.3.0"

最佳答案

根据您的要求设置 float 的格式

format = workbook.add_format({'num_format':'0.0000000'}]
更多细节:
https://xlsxwriter.readthedocs.io/example_pandas_column_formats.html

关于excel - Pandas to_excel( ) 输出 float 不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72069254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com