
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - How to Sort MultiIndex
To create a MultiIndex, use the from_arrays() method. However, to sort MultiIndex, use the multiIndex.sortlevel(). method in Pandas.
At first, import the required libraries −
import pandas as pd
MultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −
arrays = [[2, 4, 3, 1], ['John', 'Tim', 'Jacob', 'Chris']]
The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiInde −
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
Sort MultiIndex. The default sorts at level 0 −
print("\nSort MultiIndex...\n",multiIndex.sortlevel())
Example
Following is the code −
import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects # Create arrays arrays = [[2, 4, 3, 1], ['John', 'Tim', 'Jacob', 'Chris']] # The "names" parameter sets the names for each of the index levels # The from_arrays() is used to create a MultiIndex multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student')) # display the MultiIndex print("The Multi-index...\n",multiIndex) # get the levels in MultiIndex print("\nThe levels in Multi-index...\n",multiIndex.levels) # Sort MultiIndex # The default sorts at level 0 print("\nSort MultiIndex...\n",multiIndex.sortlevel())
Output
This will produce the following output −
The Multi-index... MultiIndex([(2, 'John'), (4, 'Tim'), (3, 'Jacob'), (1, 'Chris')], names=['ranks', 'student']) The levels in Multi-index... [[1, 2, 3, 4], ['Chris', 'Jacob', 'John', 'Tim']] Sort MultiIndex... (MultiIndex([(1, 'Chris'), (2, 'John'), (3, 'Jacob'), (4, 'Tim')], names=['ranks', 'student']), array([3, 0, 2, 1], dtype=int64))
- Related Articles
- Python Pandas - How to Sort MultiIndex at a specific level
- Python Pandas - How to Sort MultiIndex at a specific level in descending order
- Python Pandas - Rearrange levels in MultiIndex
- Python Pandas - Create Multiindex from arrays
- Python Pandas - Create Multiindex from dataframe
- Python Pandas - Convert Nested Dictionary to Multiindex Dataframe
- Python Pandas - Get the levels in MultiIndex
- Python Pandas - Set levels in a MultiIndex
- Python Pandas - Swap levels of a MultiIndex
- Python - Drop specific rows from multiindex Pandas Dataframe
- Python Pandas - Return MultiIndex with requested level removed
- How to do groupby on a multiindex in Pandas?
- Python Pandas - Get the Names of levels in MultiIndex
- Python Pandas - Rearrange levels using level name in MultiIndex
- Python Pandas and Numpy - Concatenate multiindex into single index

Advertisements