Skip to content

Commit edb5b3a

Browse files
committed
+pd.DataFrame.melt.
1 parent 6c17f67 commit edb5b3a

File tree

2 files changed

+103
-90
lines changed

2 files changed

+103
-90
lines changed

pandas/core/frame.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,105 @@ def unstack(self, level=-1, fill_value=None):
40324032
from pandas.core.reshape import unstack
40334033
return unstack(self, level, fill_value)
40344034

4035+
_shared_docs['melt'] = """
4036+
"Unpivots" a DataFrame from wide format to long format, optionally leaving
4037+
identifier variables set.
4038+
4039+
This function is useful to massage a DataFrame into a format where one
4040+
or more columns are identifier variables (`id_vars`), while all other
4041+
columns, considered measured variables (`value_vars`), are "unpivoted" to
4042+
the row axis, leaving just two non-identifier columns, 'variable' and
4043+
'value'.
4044+
4045+
Parameters
4046+
----------
4047+
frame : DataFrame
4048+
id_vars : tuple, list, or ndarray, optional
4049+
Column(s) to use as identifier variables.
4050+
value_vars : tuple, list, or ndarray, optional
4051+
Column(s) to unpivot. If not specified, uses all columns that
4052+
are not set as `id_vars`.
4053+
var_name : scalar
4054+
Name to use for the 'variable' column. If None it uses
4055+
``frame.columns.name`` or 'variable'.
4056+
value_name : scalar, default 'value'
4057+
Name to use for the 'value' column.
4058+
col_level : int or string, optional
4059+
If columns are a MultiIndex then use this level to melt.
4060+
4061+
See also
4062+
--------
4063+
pivot_table
4064+
DataFrame.pivot
4065+
4066+
Examples
4067+
--------
4068+
>>> import pandas as pd
4069+
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
4070+
... 'B': {0: 1, 1: 3, 2: 5},
4071+
... 'C': {0: 2, 1: 4, 2: 6}})
4072+
>>> df
4073+
A B C
4074+
0 a 1 2
4075+
1 b 3 4
4076+
2 c 5 6
4077+
4078+
>>> pd.melt(df, id_vars=['A'], value_vars=['B'])
4079+
A variable value
4080+
0 a B 1
4081+
1 b B 3
4082+
2 c B 5
4083+
4084+
>>> pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
4085+
A variable value
4086+
0 a B 1
4087+
1 b B 3
4088+
2 c B 5
4089+
3 a C 2
4090+
4 b C 4
4091+
5 c C 6
4092+
4093+
The names of 'variable' and 'value' columns can be customized:
4094+
4095+
>>> pd.melt(df, id_vars=['A'], value_vars=['B'],
4096+
... var_name='myVarname', value_name='myValname')
4097+
A myVarname myValname
4098+
0 a B 1
4099+
1 b B 3
4100+
2 c B 5
4101+
4102+
If you have multi-index columns:
4103+
4104+
>>> df.columns = [list('ABC'), list('DEF')]
4105+
>>> df
4106+
A B C
4107+
D E F
4108+
0 a 1 2
4109+
1 b 3 4
4110+
2 c 5 6
4111+
4112+
>>> pd.melt(df, col_level=0, id_vars=['A'], value_vars=['B'])
4113+
A variable value
4114+
0 a B 1
4115+
1 b B 3
4116+
2 c B 5
4117+
4118+
>>> pd.melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
4119+
(A, D) variable_0 variable_1 value
4120+
0 a B E 1
4121+
1 b B E 3
4122+
2 c B E 5
4123+
4124+
"""
4125+
4126+
@Appender(_shared_docs['melt'], indents=2)
4127+
def melt(self, id_vars=None, value_vars=None, var_name=None,
4128+
value_name='value', col_level=None):
4129+
from pandas.core.reshape import melt
4130+
return melt(self, id_vars=id_vars, value_vars=value_vars,
4131+
var_name=var_name, value_name=value_name,
4132+
col_level=col_level)
4133+
40354134
# ----------------------------------------------------------------------
40364135
# Time series-related
40374136

pandas/core/reshape.py

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import pandas.core.algorithms as algos
2727
import pandas.algos as _algos
2828

29+
from pandas.core.frame import _shared_docs
30+
from pandas.util.decorators import Appender
31+
2932
from pandas.core.index import MultiIndex, _get_na_value
3033

3134

@@ -664,98 +667,9 @@ def _convert_level_number(level_num, columns):
664667
return result
665668

666669

670+
@Appender(_shared_docs['melt'], indents=2)
667671
def melt(frame, id_vars=None, value_vars=None, var_name=None,
668672
value_name='value', col_level=None):
669-
"""
670-
"Unpivots" a DataFrame from wide format to long format, optionally leaving
671-
identifier variables set.
672-
673-
This function is useful to massage a DataFrame into a format where one
674-
or more columns are identifier variables (`id_vars`), while all other
675-
columns, considered measured variables (`value_vars`), are "unpivoted" to
676-
the row axis, leaving just two non-identifier columns, 'variable' and
677-
'value'.
678-
679-
Parameters
680-
----------
681-
frame : DataFrame
682-
id_vars : tuple, list, or ndarray, optional
683-
Column(s) to use as identifier variables.
684-
value_vars : tuple, list, or ndarray, optional
685-
Column(s) to unpivot. If not specified, uses all columns that
686-
are not set as `id_vars`.
687-
var_name : scalar
688-
Name to use for the 'variable' column. If None it uses
689-
``frame.columns.name`` or 'variable'.
690-
value_name : scalar, default 'value'
691-
Name to use for the 'value' column.
692-
col_level : int or string, optional
693-
If columns are a MultiIndex then use this level to melt.
694-
695-
See also
696-
--------
697-
pivot_table
698-
DataFrame.pivot
699-
700-
Examples
701-
--------
702-
>>> import pandas as pd
703-
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
704-
... 'B': {0: 1, 1: 3, 2: 5},
705-
... 'C': {0: 2, 1: 4, 2: 6}})
706-
>>> df
707-
A B C
708-
0 a 1 2
709-
1 b 3 4
710-
2 c 5 6
711-
712-
>>> pd.melt(df, id_vars=['A'], value_vars=['B'])
713-
A variable value
714-
0 a B 1
715-
1 b B 3
716-
2 c B 5
717-
718-
>>> pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
719-
A variable value
720-
0 a B 1
721-
1 b B 3
722-
2 c B 5
723-
3 a C 2
724-
4 b C 4
725-
5 c C 6
726-
727-
The names of 'variable' and 'value' columns can be customized:
728-
729-
>>> pd.melt(df, id_vars=['A'], value_vars=['B'],
730-
... var_name='myVarname', value_name='myValname')
731-
A myVarname myValname
732-
0 a B 1
733-
1 b B 3
734-
2 c B 5
735-
736-
If you have multi-index columns:
737-
738-
>>> df.columns = [list('ABC'), list('DEF')]
739-
>>> df
740-
A B C
741-
D E F
742-
0 a 1 2
743-
1 b 3 4
744-
2 c 5 6
745-
746-
>>> pd.melt(df, col_level=0, id_vars=['A'], value_vars=['B'])
747-
A variable value
748-
0 a B 1
749-
1 b B 3
750-
2 c B 5
751-
752-
>>> pd.melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
753-
(A, D) variable_0 variable_1 value
754-
0 a B E 1
755-
1 b B E 3
756-
2 c B E 5
757-
758-
"""
759673
# TODO: what about the existing index?
760674
if id_vars is not None:
761675
if not is_list_like(id_vars):

0 commit comments

Comments
 (0)