- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Sorting a MultiIndex
Sorting MultiIndex in Pandas is used to efficiently organize the hierarchical datasets. In Pandas MultiIndex is also known as a hierarchical index and it has multiple levels of index in Pandas data structures such as, DataFrame or Series objects. Each level in a MultiIndexed object can be sorted independently to apply the efficient slicing, indexing, filtering, and retrieving operations on your data.
Below are the key methods to sort MultiIndexed objects in Pandas −
sort_index(): Sort object by labels.
sortlevel(): Used for sorting the MultiIndexed object at a specific level.
sort_values(): Used to get the sorted copy if the DataFrame.
In this tutorial, we will learn how to sort a MultiIndexed objects in Pandas using these methods with different approaches.
Sorting MultiIndex Using sort_index()
The Pandas DataFrame.sort_index() method is used to sort a MultiIndex by all levels. Sorting a MultiIndex object can be useful for efficient indexing and slicing of the data.
Example
Here is the basic example of using the df.sort_index() method is to sort a MultiIndex by all levels. This sorts the data according to both levels of the MultiIndex.
import pandas as pd
# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')],
names=["level0", "level1"])
# Create a DataFrame
data = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])
# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)
# Sort MultiIndex with default levels
sorted_df = df.sort_index()
print("Resultant DataFrame:")
print(sorted_df)
Following is the output of the above code −
Original MultiIndexed DataFrame:
| X | Y | ||
|---|---|---|---|
| level1 | level2 | ||
| A | one | 1 | 2 |
| two | 3 | 4 | |
| three | 1 | 1 | |
| B | one | 5 | 6 |
| two | 7 | 8 | |
| three | 2 | 2 |
| X | Y | ||
|---|---|---|---|
| level1 | level2 | ||
| A | one | 1 | 2 |
| three | 1 | 1 | |
| two | 3 | 4 | |
| B | one | 5 | 6 |
| three | 2 | 2 | |
| two | 7 | 8 |
Sorting MultiIndex by Specific Level
If you want to sort by a specific level of the MultiIndex, you can use the level parameter of the df.sort_index() method.
Example
Following is the example of sorting a MultiIndex by its the first level (ie., level=0).
import pandas as pd
# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('C', 'one'), ('C', 'two'),('B', 'one'), ('B', 'two')])
# Create a DataFrame
data = [[1, 2], [3, 4], [5, 6], [7, 8]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])
# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)
# Sort MultiIndex by the first level
sorted_df = df.sort_index(level=0)
print("Resultant DataFrame:")
print(sorted_df)
Following is the output of the above code −
Original MultiIndexed DataFrame:
| X | Y | ||
|---|---|---|---|
| C | one | 1 | 2 |
| two | 3 | 4 | |
| B | one | 5 | 6 |
| two | 7 | 8 |
| X | Y | ||
|---|---|---|---|
| B | one | 5 | 6 |
| two | 7 | 8 | |
| C | one | 1 | 2 |
| two | 3 | 4 |
Sorting MultiIndex by Level Names
Similar to the above approach you can also sort the MultiIndex by level names instead of the numerical index using the df.sort_index() method with level parameter.
Example
This example sorts the MultiIndex by using the level name specified to the level parameter of the set_names() method.
import pandas as pd
# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('D', 'z'), ('D', 'x'), ('D', 'y'),('B', 't'), ('B', 's'), ('B', 'v')],
names=["level0", "level1"])
# Create a DataFrame
data = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])
# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)
# Sort by the level name
sorted_df = df.sort_index(level='level1')
print("Resultant DataFrame:")
print(sorted_df)
Following is the output of the above code −
Original MultiIndexed DataFrame:
| X | Y | ||
|---|---|---|---|
| level1 | level2 | ||
| D | z | 1 | 2 |
| x | 3 | 4 | |
| y | 1 | 1 | |
| B | t | 5 | 6 |
| s | 7 | 8 | |
| v | 2 | 2 |
| X | Y | ||
|---|---|---|---|
| level1 | level2 | ||
| B | s | 7 | 8 |
| t | 5 | 6 | |
| v | 2 | 2 | |
| D | x | 3 | 4 |
| y | 1 | 1 | |
| z | 1 | 2 |
Sorting MultiIndex at Specific Levels with sortlevel()
By using the MultiIndex.sortlevel() method you can also sort a MultiIndex at a specific level.
Example
Following is the example of sorting the MultiIndex object by using the MultiIndex.sortlevel() method.
import pandas as pd
# Create arrays
arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob']]
# 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 specific level to sort is set as a parameter i.e. level 1 here
print("\nSort MultiIndex at the requested level...\n",multiIndex.sortlevel(1))
Following is the output of the above code −
The Multi-index...
MultiIndex([(2, 'Peter'),
(4, 'Chris'),
(3, 'Andy'),
(1, 'Jacob')],
names=['ranks', 'student'])
The levels in Multi-index...
[[1, 2, 3, 4], ['Andy', 'Chris', 'Jacob', 'Peter']]
Sort MultiIndex at the requested level...
(MultiIndex([(3, 'Andy'),
(4, 'Chris'),
(1, 'Jacob'),
(2, 'Peter')],
names=['ranks', 'student']), array([2, 1, 3, 0]))
Sorting MultiIndex Using sort_values()
The sort_values() method sorts the index object and returns the copy of the index.
Example
The following example demonstrates how to sort the MultiIndex object using the sort_values() method.
import pandas as pd
# Create arrays
arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob']]
# 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)
# Sort MultiIndex using the sort_values() method
print("\nSort MultiIndex...\n",multiIndex.sort_values())
Following is the output of the above code −
The Multi-index...
MultiIndex([(2, 'Peter'),
(4, 'Chris'),
(3, 'Andy'),
(1, 'Jacob')],
names=['ranks', 'student'])
Sort MultiIndex...
MultiIndex([(1, 'Jacob'),
(2, 'Peter'),
(3, 'Andy'),
(4, 'Chris')],
names=['ranks', 'student'])