Pandas
keyboard_arrow_down 655 guides
chevron_leftMulti-index Operations Cookbook
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Resetting MultiIndex of a DataFrame in Pandas
schedule Aug 11, 2023
Last updated local_offer
Tags Python●Pandas
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
To reset the multi-index of a DataFrame, use the DataFrame's reset_index()
method.
Examples
Consider the following multi-index DataFrame:
index = [("A", "alice"), ("A", "bob"),("A", "cathy"), ("B", "david"),("B", "eric")]multi_index = pd.MultiIndex.from_tuples(index)df = pd.DataFrame({"a":[2,3,4,5,6]}, index=multi_index)df
aA alice 2 bob 3 cathy 4B david 5 eric 6
Resetting all levels
To reset the all levels of the index:
df.reset_index()
level_0 level_1 a0 A alice 21 A bob 32 A cathy 43 B david 54 B eric 6
Notice how the new columns are labelled as level_0
and level_1
.
Resetting a specific level
To reset a particular level, pass in level
like so:
df.reset_index(level=0)
level_0 aalice A 2bob A 3cathy A 4david B 5eric B 6
Related
Pandas DataFrame | reset_index method
Resets the index to the default integer index.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!