datascience.tables.Table.pivot¶
- Table.pivot(columns, rows, values=None, collect=None, zero=None)[source]¶
Generate a table with a column for each unique value in
columns, with rows for each unique value inrows. Each row counts/aggregates the values that match both row and column based oncollect.- Args:
columns– a single column label or index, (strorint),used to create new columns, based on its unique values.
rows– row labels or indices, (strorintor list),used to create new rows based on it’s unique values.
values– column label in table for use in aggregation.Default None.
collect– aggregation function, used to groupvaluesover row-column combinations. Default None.
zero– zero value to use for non-existent row-columncombinations.
- Raises:
- TypeError – if
collectis passed in andvaluesis not, vice versa.
- TypeError – if
- Returns:
New pivot table, with row-column combinations, as specified, with aggregated
valuesbycollectacross the intersection ofcolumnsandrows. Simple counts provided if values and collect are None, as default.
>>> titanic = Table().with_columns('age', make_array(21, 44, 56, 89, 95 ... , 40, 80, 45), 'survival', make_array(0,0,0,1, 1, 1, 0, 1), ... 'gender', make_array('M', 'M', 'M', 'M', 'F', 'F', 'F', 'F'), ... 'prediction', make_array(0, 0, 1, 1, 0, 1, 0, 1)) >>> titanic age | survival | gender | prediction 21 | 0 | M | 0 44 | 0 | M | 0 56 | 0 | M | 1 89 | 1 | M | 1 95 | 1 | F | 0 40 | 1 | F | 1 80 | 0 | F | 0 45 | 1 | F | 1 >>> titanic.pivot('survival', 'gender') gender | 0 | 1 F | 1 | 3 M | 3 | 1 >>> titanic.pivot('prediction', 'gender') gender | 0 | 1 F | 2 | 2 M | 2 | 2 >>> titanic.pivot('survival', 'gender', values='age', collect = np.mean) gender | 0 | 1 F | 80 | 60 M | 40.3333 | 89 >>> titanic.pivot('survival', make_array('prediction', 'gender')) prediction | gender | 0 | 1 0 | F | 1 | 1 0 | M | 2 | 0 1 | F | 0 | 2 1 | M | 1 | 1 >>> titanic.pivot('survival', 'gender', values = 'age') Traceback (most recent call last): ... TypeError: values requires collect to be specified >>> titanic.pivot('survival', 'gender', collect = np.mean) Traceback (most recent call last): ... TypeError: collect requires values to be specified