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?
We'll use the PrettyTable() class to define, modify, and print tables in Python. For databases with a Python library that conforms to the Python DB-API – an SQLite database, for example – you can define a cursor object then build a table using the from_db_cursor() function from prettytable .
Even with a Numpy vectorized code, the speed is slower than that of Julia as the complexity grows. Numpy is great for the simple methods that an array already comes with such as sum() or mean() or std() , but using logic along with them is not always straightforward and it slows the operation down considerably.
Use functions in numpy to read in tabular data. Take 2D slices of data in numpy arrays. Use 2D slices to work with particular rows or columns of data. Use the range() function in for loops.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With