Python Pandas - Advanced Reindexing with MultiIndex



In Pandas, MultiIndex or hierarchical indexing allows you to work with data structures that have multiple levels of indexing for rows and columns. When dealing with these type of structured datasets, advanced reindexing with MultiIndex becomes essential for reshaping and aligning data across different levels.

Advanced reindexing and alignment in MultiIndex DataFrames enables flexible data manipulation and reshaping in Pandas. By using methods like reindex(), swaplevel(), and reorder_levels() you can easily perform the data manipulation and restructuring tasks in Pandas.

Reindexing DataFrame with MultiIndex

Reindexing allows you to change the index of a DataFrame to match a new set of labels. The Pandas DataFrame.reindex() method is used to reindex a data along specific level of a MultiIndex.

Example

Let us see an explore of using the df.reindex() method to reindex a MultiIndexed DataFrame.

Open Compiler
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')]) # 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) # New index for reindexing new_index = [('A', 'one'), ('foo', 'two'), ('B', 'two'), ('A', 'three'), ('B', 'one'), ('A', 'two')] # Reindexing the DataFrame reindexed_df = df.reindex(new_index) print('\nReindexed DataFrame:\n', reindexed_df)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
Reindexed DataFrame:
X Y
A one 1.0 2.0
foo two NaN NaN
B two 7.0 8.0
A three 1.0 1.0
B one 5.0 6.0
A two 3.0 4.0

Changing MultiIndex Levels with swaplevel()

In a MultiIndex DataFrame, you can swap the order of the levels using the DataFrame.swaplevel() method. This is useful for reorder the levels of a DataFrame to perform operations across different hierarchical levels.

Example

The following example swaps the levels of a MultiIndexed DataFrame using the df.swaplevel() method.

Open Compiler
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')]) # 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) # Swap the levels of the original DataFrame swapped_df = df.swaplevel(0, 1, axis=0) print('\nDataFrame After Swapping Levels:\n', swapped_df)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
DataFrame After Swapping Levels:
X Y
one A 1 2
two A 3 4
three A 1 1
one B 5 6
two B 7 8
three B 2 2

Reordering MultiIndex Levels with reorder_levels()

Similar to the above approach, Pandas MultiIndex.reorder_levels() method is also used to reorder index levels of a MultiIndexed object.

Example

This example uses the Pandas MultiIndex.reorder_levels() method to reorder the levels of a MultiIndex object.

Open Compiler
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')]) # 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) # Reordering levels reordered_df = df.reorder_levels([1, 0], axis=0) print('\nDataFrame after reordering levels:\n', reordered_df)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
DataFrame after reordering levels:
X Y
one A 1 2
two A 3 4
three A 1 1
one B 5 6
two B 7 8
three B 2 2
Advertisements