datascience.tables.Table.with_columns¶
- Table.with_columns(*labels_and_values, **formatter)[source]¶
Return a table with additional or replaced columns.
- Args:
labels_and_values
: An alternating list of labels and valuesor a list of label-value pairs. If one of the labels is in existing table, then every value in the corresponding column is set to that value. If label has only a single value (
int
), every row of corresponding column takes on that value.- ‘’formatter’’ (single Formatter value): A single formatter value
that will be applied to all columns being added using this function call.
- Raises:
ValueError
: If- any label in
labels_and_values
is not a valid column name, i.e if label is not of type (str).
- any label in
- if any value in
labels_and_values
is a list/array and does not have the same length as the number of rows in the table.
- if any value in
AssertionError
:- ‘incorrect columns format’, if passed more than one sequence
(iterables) for
labels_and_values
.
- ‘even length sequence required’ if missing a pair in
label-value pairs.
- Returns:
Copy of original table with new or replaced columns. Columns added in order of labels. Equivalent to
with_column(label, value)
when passed only one label-value pair.
>>> players = Table().with_columns('player_id', ... make_array(110234, 110235), 'wOBA', make_array(.354, .236)) >>> players player_id | wOBA 110234 | 0.354 110235 | 0.236 >>> players = players.with_columns('salaries', 'N/A', 'season', 2016) >>> players player_id | wOBA | salaries | season 110234 | 0.354 | N/A | 2016 110235 | 0.236 | N/A | 2016 >>> salaries = Table().with_column('salary', ... make_array(500000, 15500000)) >>> players.with_columns('salaries', salaries.column('salary'), ... 'bonus', make_array(6, 1), formatter=_formats.CurrencyFormatter) player_id | wOBA | salaries | season | bonus 110234 | 0.354 | $500,000 | 2016 | $6 110235 | 0.236 | $15,500,000 | 2016 | $1 >>> players.with_columns(2, make_array('$600,000', '$20,000,000')) Traceback (most recent call last): ... ValueError: The column label must be a string, but a int was given >>> players.with_columns('salaries', make_array('$600,000')) Traceback (most recent call last): ... ValueError: Column length mismatch. New column does not have the same number of rows as table.