Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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 likereindex(),swaplevel(), andreorder_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 PandasDataFrame.reindex() method is used to reindex a data along specific level of a MultiIndex.

Example

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

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# New index for reindexingnew_index = [('A', 'one'), ('foo', 'two'), ('B', 'two'), ('A', 'three'), ('B', 'one'), ('A', 'two')]# Reindexing the DataFramereindexed_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 theDataFrame.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 thedf.swaplevel() method.

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# Swap the levels of the original DataFrameswapped_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, PandasMultiIndex.reorder_levels() method is also used to reorder index levels of a MultiIndexed object.

Example

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

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# Reordering levelsreordered_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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp