excel - Pandas to_excel( ) 输出 float 不正确

标签 excel pandas openpyxl xlsxwriter xlwt

我在尝试获取 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/

相关文章:

python pandas从日期时间: df ['year' ] = df ['date' ].中提取年份不起作用

python - 从 pandas DataFrame 创建术语密度矩阵的有效方法

python - Pandas 数据框中按键列分组的连续空值的计数

python - 使用openpyxl获取Excel工作表中总和的绝对值

excel - 组合 "Set"和 "If"来计数非隐藏的红细胞

python - 我们如何在 Python openpyxl 包中使用 iter_rows()?

python - 在 Openpyxl Python 中将列格式设置为百分比

Python openpyxl 空白单元格条件格式

excel - 遍历文件夹中的多个文件从特定工作表复制并粘贴到 MASTER 文件中

node.js - Node Js 中的 JSON 到 Excel 转换