4.5.7. statsmodels.iolib.table

Provides a simple table class. A SimpleTable is essentially a list of lists plus some formatting functionality.

Dependencies: the Python 2.5+ standard library.

Installation: just copy this module into your working directory (or
anywhere in your pythonpath).

Basic use:

mydata = [[11,12],[21,22]]  # data MUST be 2-dimensional
myheaders = [ "Column 1", "Column 2" ]
mystubs = [ "Row 1", "Row 2" ]
tbl = SimpleTable(mydata, myheaders, mystubs, title="Title")
print( tbl )
print( tbl.as_csv() )

A SimpleTable is inherently (but not rigidly) rectangular. You should create it from a rectangular (2d!) iterable of data. Each item in your rectangular iterable will become the data of a single Cell. In principle, items can be any object, not just numbers and strings. However, default conversion during table production is by simple string interpolation. (So you cannot have a tuple as a data item and rely on the default conversion.)

A SimpleTable allows only one column (the first) of stubs at initilization, concatenation of tables allows you to produce tables with interior stubs. (You can also assign the datatype ‘stub’ to the cells in any column, or use insert_stubs.) A SimpleTable can be concatenated with another SimpleTable or extended by another SimpleTable.


A SimpleTable can be initialized with datatypes: a list of ints that provide indexes into data_fmts and data_aligns. Each data cell is assigned a datatype, which will control formatting. If you do not specify the datatypes list, it will be set to range(ncols) where ncols is the number of columns in the data. (I.e., cells in a column have their own datatype.) This means that you can just specify data_fmts without bothering to provide a datatypes list. If len(datatypes)<ncols then datatype assignment will cycle across a row. E.g., if you provide 10 columns of data with datatypes=[0,1] then you will have 5 columns of datatype 0 and 5 columns of datatype 1, alternating. Correspoding to this specification, you should provide a list of two data_fmts and a list of two data_aligns.

Cells can be assigned labels as their datatype attribute. You can then provide a format for that lable. Us the SimpleTable’s label_cells method to do this.

def mylabeller(cell):
    if cell.data is np.nan:
        return 'missing'

print(mytable.as_text(missing='-')) Potential problems for Python 3

  • Calls next instead of __next__. The 2to3 tool should handle that no problem. (We will switch to the next function if 2.5 support is ever dropped.)
  • from __future__ import division
  • Let me know if you find other problems.
contact:alan dot isaac at gmail dot com
requires:Python 2.5.1+
note:current version
note:HTML data format currently specifies tags
todo:support a bit more of http://www.oasis-open.org/specs/tr9503.html
todo:add labels2formatters method, that associates a cell formatter with a datatype
todo:add colspan support to Cell
change:2010-05-02 eliminate newlines that came before and after table
change:2010-05-06 add label_cells to SimpleTable Functions

csv2st(csvfile[, headers, stubs, title]) Return SimpleTable instance, created from the data in csvfile, which is in comma separated values format.
iteritems(obj, **kwargs) replacement for six’s iteritems for Python2/3 compat
pad(s, width, align) Return string padded with spaces, based on alignment parameter. Classes

Cell([data, datatype, row]) Provides a table cell.
Row(seq[, datatype, table, celltype, dec_below]) Provides a table row as a list of cells.
SimpleTable(data[, headers, stubs, title, ...]) Produce a simple ASCII, CSV, HTML, or LaTeX table from a rectangular (2d!) array of data, not necessarily numerical.