@@ -28,9 +28,9 @@ Installation
28
28
Usage
29
29
-----
30
30
31
- The module provides just one function, ``tabulate ``, which takes a list
32
- of lists or a similarly shaped data structure, and outputs a nicely
33
- formatted plain-text table::
31
+ The module provides just one function, ``tabulate ``, which takes a
32
+ list of lists or another tabular data type as the first argument,
33
+ and outputs a nicely formatted plain-text table::
34
34
35
35
>>> from tabulate import tabulate
36
36
@@ -44,18 +44,21 @@ formatted plain-text table::
44
44
Mars 3390 641.85
45
45
----- ------ -------------
46
46
47
- `` tabulate `` can pretty-print two-dimensional NumPy arrays too.
47
+ The following tabular data types are supported:
48
48
49
- Examples in this file use Python2. Tabulate supports Python3 too.
49
+ * list of lists or another iterable of iterables
50
+ * dict of iterables
51
+ * two-dimensional NumPy array
52
+ * pandas.DataFrame
53
+
54
+ Examples in this file use Python2. Tabulate supports Python3 too (Python >= 3.3).
50
55
51
56
52
57
Headers
53
58
~~~~~~~
54
59
55
- If function ``tabulate `` receives two arguments, it considers the
56
- second argument to be a list of column headers.
57
- The list of headers may be passed also out-of-order with a named
58
- argument ``headers=... ``::
60
+ The second optional argument named ``headers `` defines a list of
61
+ column headers to be used::
59
62
60
63
>>> print tabulate(table, headers=["Planet","R (km)", "mass (x 10^29 kg)"])
61
64
Planet R (km) mass (x 10^29 kg)
@@ -65,6 +68,26 @@ argument ``headers=...``::
65
68
Moon 1737 73.5
66
69
Mars 3390 641.85
67
70
71
+ If `headers="firstrow", then the first row of data is used ::
72
+
73
+ >>> print tabulate([["Name","Age"],["Alice",24],["Bob",19]],
74
+ ... headers="firstrow")
75
+ Name Age
76
+ ------ -----
77
+ Alice 24
78
+ Bob 19
79
+
80
+
81
+ If `headers="keys" `, then the keys of a dictionary/dataframe,
82
+ or column indices are used::
83
+
84
+ >>> print tabulate({"Name": ["Alice", "Bob"],
85
+ ... "Age": [24, 19]}, headers="keys")
86
+ Age Name
87
+ ----- ------
88
+ 24 Alice
89
+ 19 Bob
90
+
68
91
69
92
Table format
70
93
~~~~~~~~~~~~
@@ -232,6 +255,7 @@ Performance considerations
232
255
Such features as decimal point alignment and trying to parse everything
233
256
as a number imply that ``tabulate ``:
234
257
258
+ * has to "guess" how to print a particular tabular data type
235
259
* needs to keep the entire table in-memory
236
260
* has to "transpose" the table twice
237
261
* does much more work than it may appear
@@ -244,27 +268,27 @@ separator.
244
268
245
269
In the same time ``tabulate `` is comparable to other table
246
270
pretty-printers. Given a 10x10 table (a list of lists) of mixed text
247
- and numeric data, ``tabulate `` appears to be slightly slower than
248
- ``asciitable ``, and much faster than ``PrettyTable `` and
249
- ``texttable ``
271
+ and numeric data, ``tabulate `` appears to be slower than
272
+ ``asciitable ``, and faster than ``PrettyTable `` and ``texttable ``
250
273
251
274
::
252
275
253
276
=========================== ========== ===========
254
277
Table formatter time, μs rel. time
255
278
=========================== ========== ===========
256
- join with tabs and newlines 22.7 1.0
257
- csv to StringIO 31.9 1.4
258
- asciitable (0.8.0) 841.3 37.0
259
- tabulate (0.4.4 ) 1109.8 48.9
260
- PrettyTable (0.7.1) 3795.6 167.1
261
- texttable (0.8.1) 4032.9 177.5
279
+ join with tabs and newlines 23.0 1.0
280
+ csv to StringIO 32.4 1.4
281
+ asciitable (0.8.0) 842.0 36.7
282
+ tabulate (0.5 ) 1892.9 82.5
283
+ PrettyTable (0.7.1) 3711.1 161.7
284
+ texttable (0.8.1) 3927.5 171.1
262
285
=========================== ========== ===========
263
286
264
287
265
288
Version history
266
289
---------------
267
290
291
+ - 0.5: ANSI color sequences. Printing dicts of iterables and Pandas' dataframes.
268
292
- 0.4.4: Python 2.6 support
269
293
- 0.4.3: Bug fix, None as a missing value
270
294
- 0.4.2: Fix manifest file
0 commit comments