chevron_left
Multi-index Operations Cookbook
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings
Resetting MultiIndex of a DataFrame in Pandas
Programming
chevron_rightPython
chevron_rightPandas
chevron_rightCookbooks
chevron_rightDataFrame Cookbooks
chevron_rightMulti-index Operations Cookbook
schedule Jul 1, 2022
Last updated local_offer Python●Pandas
Tags tocTable of Contents
expand_more 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.
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
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!