我正在使用包含成本字段的表格打印数据框,但是当它为零时,最低有效数字为空白。
我怀疑这个问题与浮点数和十进制数有关,但我尝试的没有任何效果。
我不确定格式化十进制输出的正确方法是什么。 下面的map参数表示浮点;我可以转换为十进制吗?
以下是相关的代码行和输出:
lists['Cost_Items'] = lists['Cost_Items'].map('{0:,.2f}'.format)
print(tabulate(lists, headers='keys', tablefmt='pipe', numalign='decimal', showindex=False))
| Item | Date_Purchased | Cost_Items |
|:---------|:-----------------|-------------:|
| Item 1 | 2018-03-31 | 10.64 |
| Item 2 | 2018-03-31 | 2.48 |
| Item 3 | 2018-03-31 | 3.1 |
| Item 4 | 2018-03-31 | 1.78 |
| Item 5 | 2018-03-31 | 2.93 |
| Item 6 | 2018-03-31 | 0.5 |
感谢任何帮助。
答案 0 :(得分:0)
我认为需要floatfmt
参数,请检查documentation:
print(tabulate(lists, headers='keys', tablefmt='pipe', floatfmt=".2f", showindex=False))
| Item | Date_Purchased | Cost_Items |
|:-------|:-----------------|-------------:|
| Item 1 | 2018-03-31 | 10.64 |
| Item 2 | 2018-03-31 | 2.48 |
| Item 3 | 2018-03-31 | 3.10 |
| Item 4 | 2018-03-31 | 1.78 |
| Item 5 | 2018-03-31 | 2.93 |
| Item 6 | 2018-03-31 | 0.50 |
print(tabulate(lists, headers='keys', tablefmt='pipe', floatfmt=".3f", showindex=False))
| Item | Date_Purchased | Cost_Items |
|:-------|:-----------------|-------------:|
| Item 1 | 2018-03-31 | 10.640 |
| Item 2 | 2018-03-31 | 2.480 |
| Item 3 | 2018-03-31 | 3.100 |
| Item 4 | 2018-03-31 | 1.780 |
| Item 5 | 2018-03-31 | 2.930 |
| Item 6 | 2018-03-31 | 0.500 |