pythonpandas

convert multi-index column to single column in dataframe


import pandas as pd

columns = pd.MultiIndex.from_tuples(
    [('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two'), ('C', '')],
    names=[None, 'number'])

df = pd.DataFrame([[1, 2, 3, 4, 'X'], [5, 6, 7, 8, 'Y']], columns=columns)

         A       B      C
number one two one two   
0        1   2   3   4  X
1        5   6   7   8  Y

I'd like to remove the multi-index by making number a column:

A   B   C   number
1   3   X   one
5   7   Y   one
2   4   X   two
6   8   Y   two

I tried extracting the values with df[[('number', ('A','one')]] so that I can assign them to individual columns, but it doesn't work.


Solution

  • Set C as the index then .stack('number').

    Then to make it look like you want, reset the index and sort by number.

    (
        df.set_index('C')
        .stack('number')
        .reset_index()
        .sort_values('number')
    )
    
       C number  A  B
    0  X    one  1  3
    2  Y    one  5  7
    1  X    two  2  4
    3  Y    two  6  8
    

    Note: To sort by number more correctly:

        .sort_values('number', key=lambda s: s.map({'one': 1, 'two': 2}))