一般来说,在 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'
来控制显示的小数位数。
关于python - 在 pandas 的 to_markdown() 中抑制科学记数法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66713432/