Ask Your Question
2

What is the process for reindexing a dataframe with a MultiIndex?

asked 2023-02-17 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-02-27 11:00:00 +0000

bukephalos gravatar image

To reindex a dataframe with a MultiIndex, you can use the reindex() method and specify the new index using a list of tuples, where each tuple contains the new index values for each level of the MultiIndex.

Here's an example:

import pandas as pd

# create a dataframe with a MultiIndex
df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8]
}, index=pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b'), ('Y', 'c'), ('Y', 'd')], names=['First', 'Second']))

# reindex the dataframe with a new MultiIndex
new_index = [('X', 'a'), ('X', 'c'), ('Y', 'b'), ('Y', 'd'), ('Z', 'e')]
new_df = df.reindex(new_index, fill_value=0)

print(new_df)

Output:

              A  B
First Second      
X     a       1  5
      c       0  0
Y     b       0  0
      d       4  8
Z     e       0  0

In this example, we create a new MultiIndex new_index and pass it to the reindex() method. We also specify fill_value=0 to fill any missing values with zeros. The resulting dataframe has the same columns as the original, but with a new index based on the new_index list. Note that any index levels that are not included in the new index will be dropped from the resulting dataframe.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-17 11:00:00 +0000

Seen: 1 times

Last updated: Feb 27 '22