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

标签 python pandas dataframe

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

但是,它们似乎都不适用于 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' 来控制显示的小数位数。

关于python - 在 pandas 的 to_markdown() 中抑制科学记数法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66713432/

相关文章:

python - Pandas : Adding new column depending on a group aggregation

python - sess.run() 的 tensorflow 值错误

python - 使用分组依据并创建 CSV

python - Pandas :在一行中向数据框添加多列

python - 如何将数据帧的第一行读取为数据行而不是标题

r - 将数据框打印到 pdf 文件

带有kwargs的函数的python类型签名(typing.Callable)

python - PIL 到 OpenCV MAT 导致颜色偏移

python - 数据帧中的时间序列丢失值

python - 尝试索引字符串列表并根据其索引删除字符串