python - 在 pandas 的 to_markdown() 中抑制科学记数法

一般来说,在 Pandas 中有很多关于抑制科学记数法的问题

  • Format / Suppress Scientific Notation from Python Pandas Aggregation Results
  • Suppressing scientific notation in pandas?
  • How do I print entire number in Python from describe() function?

但是,它们似乎都不适用于 to_markdown 函数。例如:

import pandas as pd

df = pd.DataFrame({'val': 3e10}, index=[0])  # print(df) gives 3.000000e+10
pd.set_option('float_format', '{:f}'.format) # print(df) gives 30000000000.000000

但是,df.to_markdown() 仍然产生

|    |   val |
|---:|------:|
|  0 | 3e+10 |

如何在 to_markdown() 中禁用科学记数法?

最佳答案

我在写问题的时候想出了答案。

df.to_markdown() uses tabulate在引擎盖下。因此,我们可以使用 tabulate readme 中提到的 floatfmt 参数禁用格式:

print(df.to_markdown(floatfmt=''))

产量

|    |           val |
|---:|--------------:|
|  0 | 30000000000.0 |

正如@oneextrafact 在 their answer 中指出的那样,您可以使用 floatfmt='.0f' 来控制显示的小数位数。

https://stackoverflow.com/questions/66713432/

相关文章:

python - 类型错误 : input expected at most 1 argument,

Flutter - 'showSnackBar' 已弃用 - 如何更新?

c++ - 特定的直角三角形在 Cpp 中未被识别为直角

git - 有没有没有版本提交的 'bump the version' 的方法?

c# - UWP:导航 View 没有完全延伸到标题栏

python - 有没有办法在这个正态分布的区域上色?

angular - TS2339 : Property 'text' does not exist

kotlin - 在 Kotlin 中定义数组类型

c# - 解析不同的HttpClient

Angular 11 - RouterModule - 'router-outlet' 不是已知元素