pandas 1.4.2

NotesParametersReturnsBackRef
unstack(self, level: 'Level' = -1, fill_value=None)

Returns a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels.

If the index is not a MultiIndex, the output will be a Series (the analogue of stack when the columns are not a MultiIndex).

Notes

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

Parameters

level : int, str, or list of these, default -1 (last level)

Level(s) of index to unstack, can pass level name.

fill_value : int, str or dict

Replace NaN with this value if the unstack produces missing values.

Returns

Series or DataFrame

Pivot a level of the (necessarily hierarchical) index labels.

See Also

DataFrame.pivot

Pivot a table based on column values.

DataFrame.stack

Pivot a level of the column labels (inverse operation from :None:None:`unstack`).

Examples

This example is valid syntax, but we were not able to check execution
>>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
...  ('two', 'a'), ('two', 'b')])
... s = pd.Series(np.arange(1.0, 5.0), index=index)
... s one a 1.0 b 2.0 two a 3.0 b 4.0 dtype: float64
This example is valid syntax, but we were not able to check execution
>>> s.unstack(level=-1)
     a   b
one  1.0  2.0
two  3.0  4.0
This example is valid syntax, but we were not able to check execution
>>> s.unstack(level=0)
   one  two
a  1.0   3.0
b  2.0   4.0
This example is valid syntax, but we were not able to check execution
>>> df = s.unstack(level=0)
... df.unstack() one a 1.0 b 2.0 two a 3.0 b 4.0 dtype: float64
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.frame.DataFrame.pivot pandas.core.reshape.pivot.pivot pandas.core.frame.DataFrame.stack pandas.core.reshape.melt.wide_to_long

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#8356
type: <class 'function'>
Commit: