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')
appended(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()

returns DictArray summed across columns

count(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(column)

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

filtered(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(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.
format

the display format

format_column(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(columns, **kwargs)

Return a Table with just columns

head(nrows=5)

displays top nrows

header

returns header value

items()
joined(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_other (columns_self,) – 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()
normalized(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()

returns DictArray summed across rows

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

specify policy for repr(self)

Parameters:
  • head (-) –
  • tail (-) –
  • random (-) –
sorted(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(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(nrows=5)

displays bottom nrows

to_array()
to_dataframe(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(flatten=False)

returns data as a dict

Parameters:flatten (bool) – returns a 1D dictionary
to_json()
to_normalized(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(width=500, font_size=12, layout=None, **kwargs)

returns a Plotly Table

to_rich_dict()
to_rich_html(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(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,) –
  • end, value. In that order! (start,) –
tolist(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(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(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(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(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).