2

我有这个 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 │
╘═══╧═══╛

我对更清晰的表格显示感兴趣,而忽略了我不感兴趣的值。如何打印特定值的空白单元格?例如,数组中所有 2 个值的空白,如下所示:

╒═══╤═══╕
│ 1 │   │
├───┼───┤
│ 3 │ 4 │
╘═══╧═══╛
4

1 回答 1

1

您可以转换为'U''S'dtype 并将特殊值显式替换为''

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 │
╘═══╧═══╛
于 2020-07-12T21:55:39.773 回答