NumPy: Pretty print tabular data

后端 未结 4 1538
陌清茗
陌清茗 2020-11-30 09:21

I would like to print NumPy tabular array data, so that it looks nice. R and database consoles seem to demonstrate good abilities to do this. However, NumPy\'s built-in prin

4条回答
  •  心在旅途
    2020-11-30 09:30

    I seem to be having good output with prettytable:

    from prettytable import PrettyTable
    x = PrettyTable(dat.dtype.names)
    for row in dat:
        x.add_row(row)
    # Change some column alignments; default was 'c'
    x.align['column_one'] = 'r'
    x.align['col_two'] = 'r'
    x.align['column_3'] = 'l'
    

    And the output is not bad. There is even a border switch, among a few other options:

    >>> print(x)
    +------------+---------+-------------+
    | column_one | col_two |   column_3  |
    +------------+---------+-------------+
    |          0 |  0.0001 | ABCD        |
    |          1 |  1e-005 | ABCD        |
    |          2 |  1e-006 | long string |
    |          3 |  1e-007 | ABCD        |
    +------------+---------+-------------+
    >>> print(x.get_string(border=False))
     column_one  col_two    column_3  
              0   0.0001  ABCD        
              1   1e-005  ABCD        
              2   1e-006  long string 
              3   1e-007  ABCD        
    

提交回复
热议问题