@@ -617,52 +617,88 @@ def melt(frame, id_vars=None, value_vars=None,
617
617
var_name = None , value_name = 'value' , col_level = None ):
618
618
"""
619
619
"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'.
621
627
622
628
Parameters
623
629
----------
624
630
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'.
628
639
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
630
648
631
649
Examples
632
650
--------
633
651
>>> import pandas as pd
634
652
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
635
653
... 'B': {0: 1, 1: 3, 2: 5},
636
654
... 'C': {0: 2, 1: 4, 2: 6}})
637
-
638
655
>>> df
639
656
A B C
640
657
0 a 1 2
641
658
1 b 3 4
642
659
2 c 5 6
643
660
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'])
645
668
A variable value
646
669
0 a B 1
647
670
1 b B 3
648
671
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:
649
677
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')
652
680
A myVarname myValname
653
681
0 a B 1
654
682
1 b B 3
655
683
2 c B 5
656
684
685
+ If you have multi-index columns:
686
+
657
687
>>> 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
658
694
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'])
660
696
A variable value
661
697
0 a B 1
662
698
1 b B 3
663
699
2 c B 5
664
700
665
- >>> melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
701
+ >>> pd. melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
666
702
(A, D) variable_0 variable_1 value
667
703
0 a B E 1
668
704
1 b B E 3
0 commit comments