Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas DataFrame: unstack() function

DataFrame - unstack() function

Pivot a level of the (necessarily hierarchical) index labels, returning 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).

Syntax:

DataFrame.unstack(self, level=-1, fill_value=None)

Parameters:

Name Description Type/Default Value Required / Optional
level            Level(s) of index to unstack, can pass level name int, string, or list of these(last level)
Default Value: 1
Required
fill_value  missing values replace NaN with this value if the unstack produces Required

Returns: Series or DataFrame

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - stack() function
Next: DataFrame - melt() function