Skip to content

Commit d54dc2f

Browse files
committed
+pd.DataFrame.melt.
1 parent da0523a commit d54dc2f

File tree

2 files changed

+103
-90
lines changed

2 files changed

+103
-90
lines changed

pandas/core/frame.py

+99
Original file line numberDiff line numberDiff line change
@@ -4051,6 +4051,105 @@ def unstack(self, level=-1, fill_value=None):
40514051
from pandas.core.reshape import unstack
40524052
return unstack(self, level, fill_value)
40534053

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

pandas/core/reshape.py

+4-90
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import pandas.core.algorithms as algos
2929
from pandas._libs import algos as _algos, reshape as _reshape
3030

31+
from pandas.core.frame import _shared_docs
32+
from pandas.util.decorators import Appender
33+
3134
from pandas.core.index import MultiIndex, _get_na_value
3235

3336

@@ -701,98 +704,9 @@ def _convert_level_number(level_num, columns):
701704
return result
702705

703706

707+
@Appender(_shared_docs['melt'], indents=2)
704708
def melt(frame, id_vars=None, value_vars=None, var_name=None,
705709
value_name='value', col_level=None):
706-
"""
707-
"Unpivots" a DataFrame from wide format to long format, optionally leaving
708-
identifier variables set.
709-
710-
This function is useful to massage a DataFrame into a format where one
711-
or more columns are identifier variables (`id_vars`), while all other
712-
columns, considered measured variables (`value_vars`), are "unpivoted" to
713-
the row axis, leaving just two non-identifier columns, 'variable' and
714-
'value'.
715-
716-
Parameters
717-
----------
718-
frame : DataFrame
719-
id_vars : tuple, list, or ndarray, optional
720-
Column(s) to use as identifier variables.
721-
value_vars : tuple, list, or ndarray, optional
722-
Column(s) to unpivot. If not specified, uses all columns that
723-
are not set as `id_vars`.
724-
var_name : scalar
725-
Name to use for the 'variable' column. If None it uses
726-
``frame.columns.name`` or 'variable'.
727-
value_name : scalar, default 'value'
728-
Name to use for the 'value' column.
729-
col_level : int or string, optional
730-
If columns are a MultiIndex then use this level to melt.
731-
732-
See also
733-
--------
734-
pivot_table
735-
DataFrame.pivot
736-
737-
Examples
738-
--------
739-
>>> import pandas as pd
740-
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
741-
... 'B': {0: 1, 1: 3, 2: 5},
742-
... 'C': {0: 2, 1: 4, 2: 6}})
743-
>>> df
744-
A B C
745-
0 a 1 2
746-
1 b 3 4
747-
2 c 5 6
748-
749-
>>> pd.melt(df, id_vars=['A'], value_vars=['B'])
750-
A variable value
751-
0 a B 1
752-
1 b B 3
753-
2 c B 5
754-
755-
>>> pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
756-
A variable value
757-
0 a B 1
758-
1 b B 3
759-
2 c B 5
760-
3 a C 2
761-
4 b C 4
762-
5 c C 6
763-
764-
The names of 'variable' and 'value' columns can be customized:
765-
766-
>>> pd.melt(df, id_vars=['A'], value_vars=['B'],
767-
... var_name='myVarname', value_name='myValname')
768-
A myVarname myValname
769-
0 a B 1
770-
1 b B 3
771-
2 c B 5
772-
773-
If you have multi-index columns:
774-
775-
>>> df.columns = [list('ABC'), list('DEF')]
776-
>>> df
777-
A B C
778-
D E F
779-
0 a 1 2
780-
1 b 3 4
781-
2 c 5 6
782-
783-
>>> pd.melt(df, col_level=0, id_vars=['A'], value_vars=['B'])
784-
A variable value
785-
0 a B 1
786-
1 b B 3
787-
2 c B 5
788-
789-
>>> pd.melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')])
790-
(A, D) variable_0 variable_1 value
791-
0 a B E 1
792-
1 b B E 3
793-
2 c B E 5
794-
795-
"""
796710
# TODO: what about the existing index?
797711
if id_vars is not None:
798712
if not is_list_like(id_vars):

0 commit comments

Comments
 (0)