cudf.melt#

cudf.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None)[source]#

Unpivots a DataFrame from wide format to long format, optionally leaving identifier variables set.

Parameters:
frameDataFrame
id_varstuple, list, or ndarray, optional

Column(s) to use as identifier variables. default: None

value_varstuple, list, or ndarray, optional

Column(s) to unpivot. default: all columns that are not set as id_vars.

var_namescalar

Name to use for the variable column. default: frame.columns.name or ‘variable’

value_namestr

Name to use for the value column. default: ‘value’

Returns:
outDataFrame

Melted result

Difference from pandas:
  • Does not support ‘col_level’ because cuDF does not have multi-index

Examples

>>> import cudf
>>> df = cudf.DataFrame({'A': ['a', 'b', 'c'],
...                      'B': [1, 3, 5],
...                      'C': [2, 4, 6]})
>>> df
   A  B  C
0  a  1  2
1  b  3  4
2  c  5  6
>>> cudf.melt(df, id_vars=['A'], value_vars=['B'])
   A variable  value
0  a        B      1
1  b        B      3
2  c        B      5
>>> cudf.melt(df, id_vars=['A'], value_vars=['B', 'C'])
   A variable  value
0  a        B      1
1  b        B      3
2  c        B      5
3  a        C      2
4  b        C      4
5  c        C      6

The names of ‘variable’ and ‘value’ columns can be customized:

>>> cudf.melt(df, 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