Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.