NumPy: Pretty print tabular data

青春壹個敷衍的年華 提交于 2019-11-26 11:17:40

问题


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\', \'|S12\')}
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, 0.0001, \'ABCD\') (1, 1.0000000000000001e-005, \'ABCD\')
#  (2, 9.9999999999999995e-007, \'long string\')
#  (3, 9.9999999999999995e-008, \'ABCD\')]

print(repr(dat))
# array([(0, 0.0001, \'ABCD\'), (1, 1.0000000000000001e-005, \'ABCD\'),
#        (2, 9.9999999999999995e-007, \'long string\'),
#        (3, 9.9999999999999995e-008, \'ABCD\')], 
#       dtype=[(\'column_one\', \'<i4\'), (\'col_two\', \'<f8\'), (\'column_3\', \'|S12\')])

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-005 | long string
          2 |  1e-008 | ABCD
          3 |  1e-007 | ABCD

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

I\'m using Python 2.5, NumPy 1.3.0.


回答1:


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        



回答2:


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"]

# tabulate data
table = tabulate(m, headers, tablefmt="fancy_grid")

# output
print(table)

(Above code is Python 3; for Python 2 add from __future__ import print_function at top of script)

Output:

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

The package installs via pip:

$ pip install tabulate     # (use pip3 for Python 3 on some systems)



回答3:


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




回答4:


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/



来源:https://stackoverflow.com/questions/9712085/numpy-pretty-print-tabular-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!