我在尝试获取 to_excel pandas 函数的输出时遇到了一些奇怪的情况。
我尝试读取编号为“21631706.9893399”的 xlsx excel 文件,但是当尝试写入新的 xlsx excel 文件时,输出为“21631706.98934”。我使用 openpyxl 和 xlsxwriter 进行了测试,但得到了相同的结果,但是,如果我尝试使用 xlwt 引擎编写 xls excel,它会给我正确的答案,有没有办法使用这些引擎读取和写入相同的 float ?我怀疑 xlsx 文件中存在一些 float 限制,但在 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/