pandas.DataFrame.to_markdown 将大整数转换为浮点数

问题描述

pandas.DataFrame.to_markdown 将大 int 转换为 float。这是一个错误还是一个功能?有什么解决办法吗?

>>> df = pd.DataFrame({"A": [123456,123456]})
>>> print(df.to_markdown())
|    |      A |
|---:|-------:|
|  0 | 123456 |
|  1 | 123456 |

>>> df = pd.DataFrame({"A": [1234567,1234567]})
>>> print(df.to_markdown())
|    |           A |
|---:|------------:|
|  0 | 1.23457e+06 |
|  1 | 1.23457e+06 |

>>> print(df)
         A
0  1234567
1  1234567

>>> print(df.A.dtype)
int64

解决方法

我最初只找到了一种解决方法,但没有找到解释:将列转换为字符串。

const name = "Abc";
function logName() {
  console.log('1. Name is => ',name);
  const name = "Grapes";
  console.log('2. Name is => ',name);
}
    
logName();

更新:

我认为这是由两个因素引起的:

  • tabulate 中的 >>> df = pd.DataFrame({"A": [1234567,1234567]}) >>> df["A"] = df.A.astype(str) >>> print(df.to_markdown()) | | A | |---:|--------:| | 0 | 1234567 | | 1 | 1234567 | 函数:
_column_type

可以通过def _column_type(strings,has_invisible=True,numparse=True): """The least generic type all column values are convertible to. 禁用转换来解决:

tablefmt="pretty"
  • 当有多于一列并且其中一列包含 print(df.to_markdown(tablefmt="pretty")) +---+---------+ | | A | +---+---------+ | 0 | 1234567 | | 1 | 1234567 | +---+---------+ 数字时。由于 float 使用 tabulate 来提取数据,从而将 df.values 转换为 DataFrame,因此所有值随后都会转换为相同的 numpy.array({{1} })。这也在 this issue 中讨论。
dtype
,

如果您选中 pandas 选项,则默认有效位数为 6。

import pandas as pd

pd.describe_option()

display.precision : int
    Floating point output precision (number of significant digits). This is
    only a suggestion
    [default: 6] [currently: 6]

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...