datascience.tables.Table.append_column

Table.append_column(label, values)[source]

Appends a column to the table or replaces a column.

__setitem__ is aliased to this method:
table.append_column('new_col', [1, 2, 3]) is equivalent to table['new_col'] = [1, 2, 3].
Args:

label (str): The label of the new column.

values (single value or list/array): If a single value, every

value 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.

Returns:
Original table with new or replaced column
Raises:
ValueError: If
  • label 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', ['a', 'b', 'c', 'z'],
...     'count',  [9, 3, 3, 1],
...     'points', [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', [10, 20, 30, 40])
>>> table
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')
>>> table
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, [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.