NumPy: Pretty print tabular data
Asked Answered
R

4

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 printing of tabular arrays looks like garbage:

import numpy as np
dat_dtype = {
    'names' : ('column_one', 'col_two', 'column_3'),
    'formats' : ('i', 'd', '|U12')}
dat = np.zeros(4, dat_dtype)
dat['column_one'] = range(4)
dat['col_two'] = 10**(-np.arange(4, dtype='d') - 4)
dat['column_3'] = 'ABCD'
dat['column_3'][2] = 'long string'

print(dat)
# [(0, 1.e-04, 'ABCD') (1, 1.e-05, 'ABCD') (2, 1.e-06, 'long string')
#  (3, 1.e-07, 'ABCD')]

I would like something that looks more like what a database spits out, for example, postgres-style:

 column_one | col_two |  column_3
------------+---------+-------------
          0 |  0.0001 | ABCD
          1 |   1e-05 | ABCD
          2 |   1e-08 | long string
          3 |   1e-07 | ABCD

Are there any good third-party Python libraries to format nice looking ASCII tables?

Relationship answered 14/3, 2012 at 23:52 Comment(2)
This question Python: pretty-printing ascii tables? may help.Binny
this discussion might also be interesting for you and others who end up here via google search.Quaquaversal
R
26

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-05 | ABCD        |
|          2 |   1e-06 | long string |
|          3 |   1e-07 | ABCD        |
+------------+---------+-------------+
>>> print(x.get_string(border=False))
 column_one  col_two  column_3  
          0   0.0001  ABCD        
          1    1e-05  ABCD        
          2    1e-06  long string 
          3    1e-07  ABCD        
Relationship answered 15/3, 2012 at 2:7 Comment(2)
Just wanted to add a comment that prettytable is now part of PyPI as of April 7, 2013: pypi.python.org/pypi/PrettyTable. As such you can just use pip or easy_install to install it now instead of downloading it through Google Code. BTW thanks for the tip here. +1.Rolle
It seems you need to iterate in prettytable which is a shame. Tabulate doesn't require this.Squall
P
19

The tabulate package works nicely for Numpy arrays:

import numpy as np
from tabulate import tabulate

m = np.array([[1, 2, 3], [4, 5, 6]])
headers = ["col 1", "col 2", "col 3"]

# Generate the table in fancy format.
table = tabulate(m, headers, tablefmt="fancy_grid")

# Show it.
print(table)

Output:

╒═════════╤═════════╤═════════╕
│   col 1 │   col 2 │   col 3 │
╞═════════╪═════════╪═════════╡
│       1 │       2 │       3 │
├─────────┼─────────┼─────────┤
│       4 │       5 │       6 │
╘═════════╧═════════╧═════════╛

The package can be installed from PyPI using e.g.

$ pip install tabulate
Prowel answered 12/11, 2017 at 22:55 Comment(0)
A
7

you can take advantage of array comprehension and use printf format strings:

for c1, c2, c3 in dat:  
    print "%2f | %8e | %s" % (c1, c2, c3)

https://en.wikipedia.org/wiki/Printf_format_string
And you can get even more customized if you go up to version 2.7

Analyst answered 15/3, 2012 at 1:13 Comment(0)
R
6

You might want to check out Pandas which has a lot of nice features for dealing with tabular data and seems to lay things out better when printing (It is designed be a python replacement for R):

http://pandas.pydata.org/

Radiotelegram answered 15/3, 2012 at 0:18 Comment(1)
Simplest and best solutionDrabeck

© 2022 - 2024 — McMap. All rights reserved.