eelbrain.table.melt
- eelbrain.table.melt(name, cells, cell_var_name, data, labels=None)
Restructure a Dataset such that a measured variable is in a single column
Restructure a Dataset with a certain variable represented in several columns into a longer dataset in which the variable is represented in a single column along with an identifying variable.
Additional variables are automatically included.
- Parameters
name (str) – Name of the variable in the new Dataset.
cells (Union[str, Tuple[str, ...]]) – Names of the columns representing the variable in the input Dataset. Names can either pe specified explicitly as a sequence of str, or implicitly as a str containing ‘%i’ for an integer.
cell_var_name (str) – Name of the variable to contain the cell identifier.
data (Dataset) – Input data.
labels (Union[dict, Sequence[str]]) – Labels for the keys in
cells
. Can be specified either as a{key: label}
dictionary or as a list ofstr
corresponding tocells
.
- Return type
Examples
Simple example data:
>>> data = Dataset() >>> data['y1'] = Var([1, 2, 3]) >>> data['y2'] = Var([4, 5, 6]) >>> print(data) y1 y2 ------- 1 4 2 5 3 6 >>> print(table.melt('y', ['y1', 'y2'], 'id', data)) y id ------ 1 y1 2 y1 3 y1 4 y2 5 y2 6 y2
Additional variables are automatically included:
>>> data['rm'] = Factor('abc') >>> print(data) y1 y2 rm ------------ 1 4 a 2 5 b 3 6 c >>> print(table.melt('y', ['y1', 'y2'], 'id', data)) rm y id ----------- a 1 y1 b 2 y1 c 3 y1 a 4 y2 b 5 y2 c 6 y2