@@ -617,52 +617,88 @@ def melt(frame, id_vars=None, value_vars=None,
617617 var_name = None , value_name = 'value' , col_level = None ):
618618 """
619619 "Unpivots" a DataFrame from wide format to long format, optionally leaving
620- id variables set
620+ identifier variables set.
621+
622+ This function is useful to massage a DataFrame into a format where one
623+ or more columns are identifier variables (`id_vars`), while all other
624+ columns, considered measured variables (`value_vars`), are "unpivoted" to
625+ the row axis, leaving just two non-identifier columns, 'variable' and
626+ 'value'.
621627
622628 Parameters
623629 ----------
624630 frame : DataFrame
625- id_vars : tuple, list, or ndarray
626- value_vars : tuple, list, or ndarray
627- var_name : scalar, if None uses frame.column.name or 'variable'
631+ id_vars : tuple, list, or ndarray, optional
632+ Column(s) to use as identifier variables.
633+ value_vars : tuple, list, or ndarray, optional
634+ Column(s) to unpivot. If not specified, uses all columns that
635+ are not set as `id_vars`.
636+ var_name : scalar
637+ Name to use for the 'variable' column. If None it uses
638+ ``frame.columns.name`` or 'variable'.
628639 value_name : scalar, default 'value'
629- col_level : scalar, if columns are a MultiIndex then use this level to melt
640+ Name to use for the 'value' column.
641+ col_level : int or string, optional
642+ If columns are a MultiIndex then use this level to melt.
643+
644+ See also
645+ --------
646+ pivot_table
647+ DataFrame.pivot
630648
631649 Examples
632650 --------
633651 >>> import pandas as pd
634652 >>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
635653 ... 'B': {0: 1, 1: 3, 2: 5},
636654 ... 'C': {0: 2, 1: 4, 2: 6}})
637-
638655 >>> df
639656 A B C
640657 0 a 1 2
641658 1 b 3 4
642659 2 c 5 6
643660
644- >>> melt(df, id_vars=['A'], value_vars=['B'])
661+ >>> pd.melt(df, id_vars=['A'], value_vars=['B'])
662+ A variable value
663+ 0 a B 1
664+ 1 b B 3
665+ 2 c B 5
666+
667+ >>> pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
645668 A variable value
646669 0 a B 1
647670 1 b B 3
648671 2 c B 5
672+ 3 a C 2
673+ 4 b C 4
674+ 5 c C 6
675+
676+ The names of 'variable' and 'value' columns can be customized:
649677
650- >>> melt(df, id_vars=['A'], value_vars=['B'],
651- ... var_name='myVarname', value_name='myValname')
678+ >>> pd. melt(df, id_vars=['A'], value_vars=['B'],
679+ ... var_name='myVarname', value_name='myValname')
652680 A myVarname myValname
653681 0 a B 1
654682 1 b B 3
655683 2 c B 5
656684
685+ If you have multi-index columns:
686+
657687 >>> df.columns = [list('ABC'), list('DEF')]
688+ >>> df
689+ A B C
690+ D E F
691+ 0 a 1 2
692+ 1 b 3 4
693+ 2 c 5 6
658694
659- >>> melt(df, col_level=0, id_vars=['A'], value_vars=['B'])
695+ >>> pd. melt(df, col_level=0, id_vars=['A'], value_vars=['B'])
660696 A variable value
661697 0 a B 1
662698 1 b B 3
663699 2 c B 5
664700
665- >>> melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
701+ >>> pd. melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
666702 (A, D) variable_0 variable_1 value
667703 0 a B E 1
668704 1 b B E 3
0 commit comments