@@ -4051,6 +4051,105 @@ def unstack(self, level=-1, fill_value=None):
4051
4051
from pandas .core .reshape import unstack
4052
4052
return unstack (self , level , fill_value )
4053
4053
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
+
4054
4153
# ----------------------------------------------------------------------
4055
4154
# Time series-related
4056
4155
0 commit comments