pandas 1.4.2

NotesParametersReturnsBackRef
melt(self, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level: 'Level | None' = None, ignore_index: 'bool' = True) -> 'DataFrame'

This function is useful to massage a DataFrame into a format where one or more columns are identifier variables (:None:None:`id_vars`), while all other columns, considered measured variables (:None:None:`value_vars`), are "unpivoted" to the row axis, leaving just two non-identifier columns, 'variable' and 'value'.

Notes

Reference the user guide <reshaping.melt> for more examples.

Parameters

id_vars : tuple, list, or ndarray, optional

Column(s) to use as identifier variables.

value_vars : tuple, list, or ndarray, optional

Column(s) to unpivot. If not specified, uses all columns that are not set as :None:None:`id_vars`.

var_name : scalar

Name to use for the 'variable' column. If None it uses frame.columns.name or 'variable'.

value_name : scalar, default 'value'

Name to use for the 'value' column.

col_level : int or str, optional

If columns are a MultiIndex then use this level to melt.

ignore_index : bool, default True

If True, original index is ignored. If False, the original index is retained. Index labels will be repeated as necessary.

versionadded

Returns

DataFrame

Unpivoted DataFrame.

Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.

See Also

DataFrame.explode

Explode a DataFrame from list-like columns to long format.

DataFrame.pivot

Return reshaped DataFrame organized by given index / column values.

melt

Identical method.

pivot_table

Create a spreadsheet-style pivot table as a DataFrame.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
...  'B': {0: 1, 1: 3, 2: 5},
...  'C': {0: 2, 1: 4, 2: 6}})
... df A B C 0 a 1 2 1 b 3 4 2 c 5 6
This example is valid syntax, but we were not able to check execution
>>> df.melt(id_vars=['A'], value_vars=['B'])
   A variable  value
0  a        B      1
1  b        B      3
2  c        B      5
This example is valid syntax, but we were not able to check execution
>>> df.melt(id_vars=['A'], value_vars=['B', 'C'])
   A variable  value
0  a        B      1
1  b        B      3
2  c        B      5
3  a        C      2
4  b        C      4
5  c        C      6

The names of 'variable' and 'value' columns can be customized:

This example is valid syntax, but we were not able to check execution
>>> df.melt(id_vars=['A'], value_vars=['B'],
...  var_name='myVarname', value_name='myValname') A myVarname myValname 0 a B 1 1 b B 3 2 c B 5

Original index values can be kept around:

This example is valid syntax, but we were not able to check execution
>>> df.melt(id_vars=['A'], value_vars=['B', 'C'], ignore_index=False)
   A variable  value
0  a        B      1
1  b        B      3
2  c        B      5
0  a        C      2
1  b        C      4
2  c        C      6

If you have multi-index columns:

This example is valid syntax, but we were not able to check execution
>>> df.columns = [list('ABC'), list('DEF')]
... df A B C D E F 0 a 1 2 1 b 3 4 2 c 5 6
This example is valid syntax, but we were not able to check execution
>>> df.melt(col_level=0, id_vars=['A'], value_vars=['B'])
   A variable  value
0  a        B      1
1  b        B      3
2  c        B      5
This example is valid syntax, but we were not able to check execution
>>> df.melt(id_vars=[('A', 'D')], value_vars=[('B', 'E')])
  (A, D) variable_0 variable_1  value
0      a          B          E      1
1      b          B          E      3
2      c          B          E      5
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.explode pandas.core.reshape.pivot.pivot_table pandas.core.frame.DataFrame.pivot_table pandas.core.reshape.melt.melt pandas.core.series.Series.explode pandas.core.frame.DataFrame.melt

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#8423
type: <class 'function'>
Commit: