eelbrain.table.melt

eelbrain.table.melt(name, cells, cell_var_name, ds, 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 : sequence of str | 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.

ds : Dataset

Input Dataset.

labels : dict | sequence of str

Labels for the keys in cells. Can be specified either as a {key: label} dictionary or as a list of str corresponding to cells.

Examples

Simple example data:

>>> ds = Dataset()
>>> ds['y1'] = Var([1, 2, 3])
>>> ds['y2'] = Var([4, 5, 6])
>>> print(ds)
y1   y2
-------
1    4
2    5
3    6
>>> print(table.melt('y', ['y1', 'y2'], 'id', ds))
y   id
------
1   y1
2   y1
3   y1
4   y2
5   y2
6   y2

Additional variables are automatically included:

>>> ds['rm'] = Factor('abc')
>>> print(ds)
y1   y2   rm
------------
1    4    a
2    5    b
3    6    c
>>> print(table.melt('y', ['y1', 'y2'], 'id', ds))
rm   y   id
-----------
a    1   y1
b    2   y1
c    3   y1
a    4   y2
b    5   y2
c    6   y2