datascience.tables.Table.apply¶
-
Table.
apply
(fn, column_label=None)[source]¶ Returns an array where
fn
is applied to each set of elements by row from the specified columns incolumn_label
. If no column_label is specified, then each row is passed to fn.- Args:
fn
(function): The function to be applied to elements specified- by
column_label
. column_label
(single string or list of strings): Names of- columns to be passed into function
fn
. Length must match number of elementsfn
takes.
- Raises:
ValueError
: column name incolumn_label
is not an existing- column in the table.
- Returns:
- A numpy array consisting of results of applying
fn
to elements specified bycolumn_label
in each row.
>>> t = Table().with_columns([ ... 'letter', ['a', 'b', 'c', 'z'], ... 'count', [9, 3, 3, 1], ... 'points', [1, 2, 2, 10]]) >>> t letter | count | points a | 9 | 1 b | 3 | 2 c | 3 | 2 z | 1 | 10 >>> t.apply(lambda x: x - 1, 'points') array([0, 1, 1, 9]) >>> t.apply(lambda x, y: x * y, ['count', 'points']) array([ 9, 6, 6, 10])
Whole rows are passed to the function if no columns are specified.
>>> t.apply(lambda row: row.item('count') * 2) array([18, 6, 6, 2])