Table

class cogent3.util.table.Table(header=None, rows=None, row_order=None, digits=4, space=4, title='', missing_data='', max_width=1e+100, row_ids=None, legend='', column_templates=None, dtype=None, data_frame=None, format='simple')
Attributes
format

the display format

header

returns header value

Methods

appended(self, new_column, \*tables, \*\*kwargs)

Append an arbitrary number of tables to the end of this one.

col_sum(self)

returns DictArray summed across columns

count(self, callback[, columns])

Returns number of rows for which the provided callback function returns True when passed row data from columns.

distinct_values(self, column)

returns the set of distinct values for the named column(s)

filtered(self, callback[, columns])

Returns a sub-table of rows for which the provided callback function returns True when passed row data from columns.

filtered_by_column(self, callback, \*\*kwargs)

Returns a table with columns identified by callback

format_column(self, column_head, format_template)

Provide a formatting template for a named column.

get_columns(self, columns, \*\*kwargs)

Return a Table with just columns

head(self[, nrows])

displays top nrows

joined(self, other_table[, columns_self, …])

returns a new table containing the join of this table and other_table.

normalized(self[, by_row, denominator_func])

returns a table with elements expressed as a fraction according to the results from func

row_sum(self)

returns DictArray summed across rows

set_repr_policy(self[, head, tail, random])

specify policy for repr(self)

sorted(self[, columns, reverse])

Returns a new table sorted according to columns order.

summed(self[, indices, col_sum, strict])

returns the sum of numerical values for column(s)/row(s)

tail(self[, nrows])

displays bottom nrows

to_dataframe(self[, categories])

returns pandas DataFrame instance

to_dict(self[, flatten])

returns data as a dict

to_normalized(self[, by_row, by_column])

returns a DictArray as frequencies

to_plotly(self[, width, font_size, layout])

returns a Plotly Table

to_rich_html(self[, row_cell_func, …])

returns just the table html code.

to_string(self[, format, borders, sep, center])

Return the table as a formatted string.

tolist(self[, columns])

Returns raw data as a 1D or 2D list of rows from columns.

transposed(self, new_column_name[, …])

returns the transposed table.

with_new_column(self, new_column, callback)

Returns a new table with an additional column, computed using callback.

with_new_header(self, old, new, \*\*kwargs)

returns a new Table with old header labels replaced by new

write(self, filename[, mode, writer, …])

Write table to filename in the specified format.

items

keys

to_array

to_json

to_rich_dict

appended(self, new_column, *tables, **kwargs)

Append an arbitrary number of tables to the end of this one. Returns a new table object. Optional keyword arguments to the new tables constructor may be passed.

Parameters
new_column

provide a heading for the new column, each tables title will be placed in it. If value is false, the result is no additional column.

col_sum(self)

returns DictArray summed across columns

count(self, callback, columns=None, **kwargs)

Returns number of rows for which the provided callback function returns True when passed row data from columns. Row data is a 1D list if more than one column, raw row[col] value otherwise.

Parameters
columns

the columns whose values determine whether a row is to be included.

callback

Can be a function, which takes the sub by columns and returns True/False, or a string representing valid python code to be evaluated.

distinct_values(self, column)

returns the set of distinct values for the named column(s)

filtered(self, callback, columns=None, **kwargs)

Returns a sub-table of rows for which the provided callback function returns True when passed row data from columns. Row data is a 1D list if more than one column, raw row[col] value otherwise.

Parameters
columns

the columns whose values determine whether a row is to be included.

callback

Can be a function, which takes the sub by columns and returns True/False, or a string representing valid python code to be evaluated.

filtered_by_column(self, callback, **kwargs)

Returns a table with columns identified by callback

Parameters
callback

A function which takes the columns delimited by columns and returns True/False, or a string representing valid python code to be evaluated.

property format

the display format

format_column(self, column_head, format_template)

Provide a formatting template for a named column.

Parameters
column_head

the column label.

format_template

string formatting template or a function that will handle the formatting.

get_columns(self, columns, **kwargs)

Return a Table with just columns

head(self, nrows=5)

displays top nrows

property header

returns header value

items(self)
joined(self, other_table, columns_self=None, columns_other=None, inner_join=True, **kwargs)

returns a new table containing the join of this table and other_table. Default behaviour is the natural inner join. Checks for equality in the specified columns (if provided) or all columns; a combined row is included in the output if all indices match exactly. A combined row contains first the row of this table, and then columns from the other_table that are not key columns (i.e. not specified in columns_other). The order (of self, then other) is preserved. The column headers of the output are made unique by replacing the headers of other_table with <other_table.Title>_<other_table.header>.

Parameters
other_table

A table object which will be joined with this table. other_table must have a title.

columns_self, columns_other

indices of key columns that will be compared in the join operation. Can be either column index, or a string matching the column header. The order matters, and the dimensions of columns_self and columns_other have to match. A row will be included in the output iff self[row][columns_self[i]]==other_table[row][columns_other[i]] for all i

inner_join

if False, the outer join of the two tables is returned.

keys(self)
normalized(self, by_row=True, denominator_func=None, **kwargs)

returns a table with elements expressed as a fraction according to the results from func

Parameters
by_row

normalisation done by row

denominator_func

a callback function that takes an array and returns a value to be used as the denominator. Default is sum.

row_sum(self)

returns DictArray summed across rows

set_repr_policy(self, head=None, tail=None, random=0)

specify policy for repr(self)

Parameters
- head: number of top rows to included in represented display
- tail: number of bottom rows to included in represented display
- random: number of rows to sample randomly (supercedes head/tail)
sorted(self, columns=None, reverse=None, **kwargs)

Returns a new table sorted according to columns order.

If only reverse is provided, that order is used.

Parameters
columns

column headings, their order determines the sort order.

reverse

column headings, these columns will be reverse sorted.

Either can be provided as just a single string, or a series of strings.

summed(self, indices=None, col_sum=True, strict=True, **kwargs)

returns the sum of numerical values for column(s)/row(s)

Parameters
indices

column name(s) or indices or row indices

col_sum

sums values in the indicated column, the default. If False, returns the row sum.

strict

if False, ignores cells with non column/row.

tail(self, nrows=5)

displays bottom nrows

to_array(self)
to_dataframe(self, categories=None)

returns pandas DataFrame instance

Parameters
categories

converts these columns to category dtype in the data frame. Note, categories are not ordered.

to_dict(self, flatten=False)

returns data as a dict

Parameters
flattenbool

returns a 1D dictionary

to_json(self)
to_normalized(self, by_row=False, by_column=False)

returns a DictArray as frequencies

Parameters
by_row

rows sum to 1

by_col

columns sum to 1

to_plotly(self, width=500, font_size=12, layout=None, **kwargs)

returns a Plotly Table

to_rich_dict(self)
to_rich_html(self, row_cell_func=None, header_cell_func=None, element_formatters=None, merge_identical=False, compact=False)

returns just the table html code.

Parameters
row_cell_func

callback function that formats the row values. Must take the row value and coordinates (row index, column index).

header_cell_func

callback function that formats the column headings must take the header label value and coordinate

element_formatters

a dictionary of specific callback funcs for formatting individual html table elements. e.g. {‘table’: lambda x: ‘<table border=”1” class=”docutils”>’}

merge_identical

cells within a row are merged to one span.

to_string(self, format='', borders=True, sep=None, center=False, **kwargs)

Return the table as a formatted string.

Parameters
format

possible formats are ‘rest’/’rst’, ‘markdown’/’md’, ‘latex’, ‘html’, ‘phylip’, ‘bedgraph’, ‘csv’, ‘tsv’, or ‘simple’ (default).

sep

A string separator for delineating columns, e.g. ‘,’ or ‘ ‘. Overrides format.

center

content is centered in the column, default is right justified

NOTE: If format is bedgraph, assumes that column headers are chrom,
start, end, value. In that order!
tolist(self, columns=None)

Returns raw data as a 1D or 2D list of rows from columns. If one column, its a 1D list.

Parameters
columns

if None, all data are returned

transposed(self, new_column_name, select_as_header=None, **kwargs)

returns the transposed table.

Parameters
new_column_name

the existing header will become a column with this name

select_as_header

current column name containing data to be used as the header. Defaults to the first column.

with_new_column(self, new_column, callback, columns=None, **kwargs)

Returns a new table with an additional column, computed using callback.

Parameters
new_column

new column heading

columns

the columns whose values determine whether a row is to be included.

callback

Can be a function, which takes the sub by columns and returns True/False, or a string representing valid python code to be evaluated.

with_new_header(self, old, new, **kwargs)

returns a new Table with old header labels replaced by new

Parameters
old

the old column header(s). Can be a string or series of them.

new

the new column header(s). Can be a string or series of them.

write(self, filename, mode=None, writer=None, format=None, sep=None, compress=None, **kwargs)

Write table to filename in the specified format. If a format is not specified, it attempts to use a filename suffix. Note if a sep argument is provided, unformatted values are written to file in order to preserve numerical accuracy.

Parameters
mode

file opening mode

format

Valid formats are those of the to_string method plus pickle. Will try and guess from filename if not specified.

writer

a function for formatting the data for output.

sep

a character delimiter for fields.

compress

if True, gzips the file and appends .gz to the filename (if not already added).