datascience.tables.Table.append_column¶
- Table.append_column(label, values, formatter=None)[source]¶
Appends a column to the table or replaces a column.
__setitem__
is aliased to this method:table.append_column('new_col', make_array(1, 2, 3))
is equivalent totable['new_col'] = make_array(1, 2, 3)
.- Args:
label
(str): The label of the new column.values
(single value or list/array): If a single value, everyvalue in the new column is
values
.If a list or array, the new column contains the values in
values
, which must be the same length as the table.formatter
(single formatter): Adds a formatter to the column beingappended. No formatter added by default.
- Returns:
Original table with new or replaced column
- Raises:
ValueError
: Iflabel
is not a string.values
is a list/array and does not have the same length as the number of rows in the table.
>>> table = Table().with_columns( ... 'letter', make_array('a', 'b', 'c', 'z'), ... 'count', make_array(9, 3, 3, 1), ... 'points', make_array(1, 2, 2, 10)) >>> table letter | count | points a | 9 | 1 b | 3 | 2 c | 3 | 2 z | 1 | 10 >>> table.append_column('new_col1', make_array(10, 20, 30, 40)) letter | count | points | new_col1 a | 9 | 1 | 10 b | 3 | 2 | 20 c | 3 | 2 | 30 z | 1 | 10 | 40 >>> table.append_column('new_col2', 'hello') letter | count | points | new_col1 | new_col2 a | 9 | 1 | 10 | hello b | 3 | 2 | 20 | hello c | 3 | 2 | 30 | hello z | 1 | 10 | 40 | hello >>> table.append_column(123, make_array(1, 2, 3, 4)) Traceback (most recent call last): ... ValueError: The column label must be a string, but a int was given >>> table.append_column('bad_col', [1, 2]) Traceback (most recent call last): ... ValueError: Column length mismatch. New column does not have the same number of rows as table.