MCPcopy
hub / github.com/pandas-dev/pandas / melt

Function melt

pandas/core/reshape/melt.py:45–278  ·  view source on GitHub ↗

Unpivot a DataFrame from wide to long format, optionally leaving identifiers set. This function is useful to reshape a DataFrame into a format where one or more columns are identifier variables (`id_vars`), while all other columns are considered measured variables (`value_vars`), a

(
    frame: DataFrame,
    id_vars=None,
    value_vars=None,
    var_name=None,
    value_name: Hashable = "value",
    col_level=None,
    ignore_index: bool = True,
)

Source from the content-addressed store, hash-verified

43
44@set_module("pandas")
45def melt(
46 frame: DataFrame,
47 id_vars=None,
48 value_vars=None,
49 var_name=None,
50 value_name: Hashable = "value",
51 col_level=None,
52 ignore_index: bool = True,
53) -> DataFrame:
54 """
55 Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.
56
57 This function is useful to reshape a DataFrame into a format where one
58 or more columns are identifier variables (`id_vars`), while all other
59 columns are considered measured variables (`value_vars`), and are "unpivoted" to
60 the row axis, leaving just two non-identifier columns, 'variable' and
61 'value'.
62
63 Parameters
64 ----------
65 frame : DataFrame
66 The DataFrame to unpivot.
67 id_vars : scalar, tuple, list, or ndarray, optional
68 Column(s) to use as identifier variables.
69 value_vars : scalar, tuple, list, or ndarray, optional
70 Column(s) to unpivot. If not specified, uses all columns that
71 are not set as `id_vars`.
72 var_name : scalar, tuple, list, or ndarray, optional
73 Name to use for the 'variable' column. If None it uses
74 ``frame.columns.name`` or 'variable'. Must be a scalar if columns are a
75 MultiIndex.
76 value_name : scalar, default 'value'
77 Name to use for the 'value' column, can't be an existing column label.
78 col_level : scalar, optional
79 If columns are a MultiIndex then use this level to melt.
80 ignore_index : bool, default True
81 If True, original index is ignored. If False, the original index is retained.
82 Index labels will be repeated as necessary.
83
84 Returns
85 -------
86 DataFrame
87 Unpivoted DataFrame.
88
89 See Also
90 --------
91 DataFrame.melt : Identical method.
92 pivot_table : Create a spreadsheet-style pivot table as a DataFrame.
93 DataFrame.pivot : Return reshaped DataFrame organized
94 by given index / column values.
95 DataFrame.explode : Explode a DataFrame from list-like
96 columns to long format.
97
98 Notes
99 -----
100 Reference :ref:`the user guide <reshaping.melt>` for more examples.
101
102 Examples

Callers 12

time_melt_dataframeMethod · 0.90
meltMethod · 0.90
test_top_level_methodMethod · 0.90
test_pandas_dtypesMethod · 0.90
test_ignore_indexMethod · 0.90
melt_stubFunction · 0.85

Calls 13

concatFunction · 0.90
ensure_list_varsFunction · 0.85
get_indexer_forMethod · 0.80
get_level_valuesMethod · 0.80
anyMethod · 0.45
uniqueMethod · 0.45
copyMethod · 0.45
popMethod · 0.45
ravelMethod · 0.45
repeatMethod · 0.45
_get_level_valuesMethod · 0.45
_constructorMethod · 0.45