modin.pandas.DataFrame.melt¶
- DataFrame.melt(id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None, ignore_index=True)[source]¶
Unpivot a
DataFrame
from wide to long format, optionally leaving identifiers set.- Parameters:
id_vars (list of identifiers to retain in the result) –
value_vars (list of columns to unpivot on) – defaults to all columns, excluding the id_vars columns
var_name (variable name, defaults to "variable") –
value_name (value name, defaults to "value") –
col_level (int, not implemented) –
ignore_index (bool) –
- Returns:
unpivoted on the value columns
- Return type:
Examples
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'}, ... 'B': {0: 1, 1: 3, 2: 5}, ... 'C': {0: 2, 1: 4, 2: 6}}) >>> df A B C 0 a 1 2 1 b 3 4 2 c 5 6
>>> df.melt() variable value 0 A a 1 A b 2 A c 3 B 1 4 B 3 5 B 5 6 C 2 7 C 4 8 C 6
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'}, ... 'B': {0: 1, 1: 3, 2: 5}, ... 'C': {0: 2, 1: 4, 2: 6}}) >>> df.melt(id_vars=['A'], value_vars=['B'], var_name='myVarname', value_name='myValname') A myVarname myValname 0 a B 1 1 b B 3 2 c B 5