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.

 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: 
XY
Aone12
two34
three11
Bone56
two78
three22
Reindexed DataFrame:
XY
Aone1.02.0
footwoNaNNaN
Btwo7.08.0
Athree1.01.0
Bone5.06.0
Atwo3.04.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.

 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: 
XY
Aone12
two34
three11
Bone56
two78
three22
DataFrame After Swapping Levels:
XY
oneA12
twoA34
threeA11
oneB56
twoB78
threeB22

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.

 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: 
XY
Aone12
two34
three11
Bone56
two78
three22
DataFrame after reordering levels:
XY
oneA12
twoA34
threeA11
oneB56
twoB78
threeB22
Advertisements
close