gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:34:23 28 4
gpt4 key购买 nike

一般来说,在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com