datascience.tables.Table.stack

Table.stack(key, labels=None)[source]

Takes k original columns and returns two columns, with col. 1 of all column names and col. 2 of all associated data.

Args:
key: Name of a column from table which is the basis for stacking

values from the table.

labels: List of column names which must be included in the stacked

representation of the table. If no value is supplied for this argument, then the function considers all columns from the original table.

Returns:

A table whose first column consists of stacked values from column passed in key. The second column of this returned table consists of the column names passed in labels, whereas the final column consists of the data values corresponding to the respective values in the first and second columns of the new table.

Examples:

>>> t = Table.from_records([
...   {
...    'column1':'data1',
...    'column2':86,
...    'column3':'b',
...    'column4':5,
...   },
...   {
...    'column1':'data2',
...    'column2':51,
...    'column3':'c',
...    'column4':3,
...   },
...   {
...    'column1':'data3',
...    'column2':32,
...    'column3':'a',
...    'column4':6,
...   }
... ])
>>> t
column1 | column2 | column3 | column4
data1   | 86      | b       | 5
data2   | 51      | c       | 3
data3   | 32      | a       | 6
>>> t.stack('column2')
column2 | column  | value
86      | column1 | data1
86      | column3 | b
86      | column4 | 5
51      | column1 | data2
51      | column3 | c
51      | column4 | 3
32      | column1 | data3
32      | column3 | a
32      | column4 | 6
>>> t.stack('column2',labels=['column4','column1'])
column2 | column  | value
86      | column1 | data1
86      | column4 | 5
51      | column1 | data2
51      | column4 | 3
32      | column1 | data3
32      | column4 | 6