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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/dataframe/dataframe-unstack.php