python表格:显示特定值的空白单元格
我有这个numpy阵列python表格:显示特定值的空白单元格,python,numpy,pretty-print,tabulate,Python,Numpy,Pretty Print,Tabulate,我有这个numpy阵列 import numpy as np from tabulate import tabulate data = np.array([[1,2], [3,4]]) data2 = tabulate(data, tablefmt='fancy_grid') print(data2) ╒═══╤═══╕ │ 1 │ 2 │ ├───┼───┤ │ 3 │ 4 │ ╘═══╧═══╛ 我对表格的更清晰显示感兴趣,而忽略了我不感兴趣的值。如何打印特定值的空白单元格?例如,数
import numpy as np
from tabulate import tabulate
data = np.array([[1,2], [3,4]])
data2 = tabulate(data, tablefmt='fancy_grid')
print(data2)
╒═══╤═══╕
│ 1 │ 2 │
├───┼───┤
│ 3 │ 4 │
╘═══╧═══╛
我对表格的更清晰显示感兴趣,而忽略了我不感兴趣的值。如何打印特定值的空白单元格?例如,数组中所有2个值都为空,如下所示:
╒═══╤═══╕
│ 1 │ │
├───┼───┤
│ 3 │ 4 │
╘═══╧═══╛
您可以转换为
'U'
或'S'
数据类型,并将特殊值显式替换为'
:
您可以编写一个包装函数,将numpy数组转换为列表列表,然后手动将某个元素的所有内容更改为
”
.Awesome,从源代码处进行修改。很有效,谢谢!
from tabulate import tabulate
data = np.array([[1,2], [3,4]])
data2 = tabulate(np.where(data==2,'',data.astype('U')), tablefmt='fancy_grid')
print(data2)
╒═══╤═══╕
│ 1 │ │
├───┼───┤
│ 3 │ 4 │
╘═══╧═══╛