[英]python pandas tabulate not printing least significant digit when zero
我正在使用包含成本字段的表格來打印數據框,但最低有效位為零時為空白。
我懷疑問題與浮點數與十進制數有關,但我嘗試過的任何方法都沒有奏效。
我不確定格式化十進制輸出的正確方法是什么。 下面的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 |
任何幫助表示贊賞。
我相信需要floatfmt
參數,請檢查文檔 :
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 |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.