Turn Pandas Multi-Index into column
Asked Answered
C

8

265

I have a dataframe with 2 index levels:

                         value
Trial    measurement
    1              0        13
                   1         3
                   2         4
    2              0       NaN
                   1        12
    3              0        34 

Which I want to turn into this:

Trial    measurement       value

    1              0        13
    1              1         3
    1              2         4
    2              0       NaN
    2              1        12
    3              0        34 

How can I best do this?

I need this because I want to aggregate the data as instructed here, but I can't select my columns like that if they are in use as indices.

Chicken answered 21/11, 2013 at 0:37 Comment(3)
Duplicate: #18624539 You want the first suggestion. .reset_index()I
many thanks, I actually browsed around for this a lot, but "make multiindex to column" and similar queries always got me threads which wanted to pivot their dataframes...Chicken
Always easier to find an answer when you already know it :)I
K
329

The reset_index() is a pandas DataFrame method that will transfer index values into the DataFrame as columns. The default setting for the parameter is drop=False (which will keep the index values as columns).

All you have to do call .reset_index() after the name of the DataFrame:

df = df.reset_index()  
Kenwrick answered 8/9, 2014 at 21:42 Comment(4)
For my case where I had 3 index levels inplace reset did not work. Alternative is assigning newly resetted dataframe to a new one: df2 = df.reset_index()Barboza
To reset only a particular level(s), use df.reset_index(level=[...])Threlkeld
Or the side-effect (probably quicker) way: df.reset_index(inplace=True)Blench
df.reset_index(names=['a', 'b']) to provide names/alternative names to the produced columns.Introspection
H
38

This doesn't really apply to your case but could be helpful for others (like myself 5 minutes ago) to know. If one's multindex have the same name like this:

                         value
Trial        Trial
    1              0        13
                   1         3
                   2         4
    2              0       NaN
                   1        12
    3              0        34 

df.reset_index(inplace=True) will fail, cause the columns that are created cannot have the same names.

So then you need to rename the multindex with df.index = df.index.set_names(['Trial', 'measurement']) to get:

                           value
Trial    measurement       

    1              0        13
    1              1         3
    1              2         4
    2              0       NaN
    2              1        12
    3              0        34 

And then df.reset_index(inplace=True) will work like a charm.

I encountered this problem after grouping by year and month on a datetime-column(not index) called live_date, which meant that both year and month were named live_date.

Hemminger answered 17/11, 2017 at 17:46 Comment(1)
How to have your Trial values to repeat themselves? I had the same problem and it works except my values don't repeat themselves.Disserve
U
25

There may be situations when df.reset_index() cannot be used (e.g., when you need the index, too). In this case, use index.get_level_values() to access index values directly:

df['Trial'] = df.index.get_level_values(0)
df['measurement'] = df.index.get_level_values(1)

This will assign index values to individual columns and keep the index.

See the docs for further info.

Urgency answered 27/10, 2020 at 8:39 Comment(1)
This is soooooooooo useful! It should be possible to do this using much clearer language, e.g. df['measurement'] = df.index.values(1).Atmospherics
V
19

As @cs95 mentioned in a comment, to drop only one level, use:

df.reset_index(level=[...])

This avoids having to redefine your desired index after reset.

Villiform answered 20/5, 2019 at 0:6 Comment(0)
F
5

I ran into Karl's issue as well. I just found myself renaming the aggregated column then resetting the index.

df = pd.DataFrame(df.groupby(['arms', 'success'])['success'].sum()).rename(columns={'success':'sum'})

enter image description here

df = df.reset_index()

enter image description here

Fleer answered 28/9, 2020 at 16:37 Comment(0)
P
3

Short and simple

df2 = pd.DataFrame({'test_col': df['test_col'].describe()})
df2 = df2.reset_index()
Pongee answered 16/5, 2022 at 13:42 Comment(0)
J
1

Similar to Alex solution in a more generalized form. It keeps the indexes untouched and adds the index levels as new columns with its name.

for i in df.index.names:
    df[i] = df.index.get_level_values(i)

which gives the new columns 'Trial' and 'measurement'

                   value Trial    measurement
Trial measurement             
    1           0     13     1              0     
                1      3     1              1     
                2      4     1              2     
  ...  
Jovanjove answered 30/10, 2022 at 0:13 Comment(0)
A
1

A solution that might be helpful in cases when not every column has multiple index levels:

df.columns = df.columns.map(''.join)
Amygdaloid answered 17/1, 2023 at 13:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.