我正在将pandas数据框输出到我正在编写的报告中,这很容易,因为您现在可以df.to_markdown()
将数据框转换为标记表,然后pandoc可以生成报告
通过将floatfmt=".3g"
作为参数传递,我们可以非常精细地控制特定的格式细节,该参数的操作解释得非常清楚here:
... The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then, if m <= exp < p, where m is -4 for floats and -6 for Decimals, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it, unless the '#' option is used...
除了我不想删除尾随的零,我也看不到一个简单的方法来防止它们被删除
如果您想要一个可复制的示例,请点击这里:
import pandas as pd
df = pd.DataFrame({'Numbers':[0.100, 0.123, 0.101, 0.099, 0.120, 0.012]})
print(df.to_markdown(showindex=False, floatfmt=".2g"))
给予
| Numbers |
|----------:|
| 0.1 |
| 0.12 |
| 0.1 |
| 0.099 |
| 0.12 |
| 0.012 |
我可以将其更改为指定十进制数字,如下所示
print(df.to_markdown(showindex=False, floatfmt=".2f"))
给
| Numbers |
|----------:|
| 0.10 |
| 0.12 |
| 0.10 |
| 0.10 |
| 0.12 |
| 0.01 |
--但那不是我想要的。我想要两个重要的数字。带尾随零。像这样:
| Numbers |
|----------:|
| 0.10 |
| 0.12 |
| 0.10 |
| 0.099 |
| 0.12 |
| 0.012 |
为了保持有效的尾随零,使用
#
所以改变
到
相关问题 更多 >
编程相关推荐