问题描述
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]