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
- Exercises: Weekly Top 12 Most Popular Topics
- Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
- SQL Exercises, Practice, Solution - SUBQUERIES
- C Programming Exercises, Practice, Solution : For Loop
- Python Exercises, Practice, Solution
- Python Data Type: List - Exercises, Practice, Solution
- C++ Basic: Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - exercises on Employee Database
- SQL Exercises, Practice, Solution - exercises on Movie Database
- SQL Exercises, Practice, Solution - exercises on Soccer Database
- C Programming Exercises, Practice, Solution : Recursion