How to use reset_index() function In Python Pandas
Pandas provide a function called reset_index() to flatten the hierarchical index created due to the groupby aggregation function in Python.
Syntax: pandas.DataFrame.reset_index(level, drop, inplace)
Parameters:
- level – removes only the specified levels from the index
- drop – resets the index to the default integer index
- inplace – modifies the dataframe object permanently without creating a copy.
Example:
In this example, We used the pandas groupby function to group car sales data by quarters and reset_index() pandas function to flatten the hierarchical indexed columns of the grouped dataframe.
Python3
# import the python pandas package import pandas as pd # create a sample dataframe data = pd.DataFrame({ "cars" : [ "bmw" , "bmw" , "benz" , "benz" ], "sale_q1 in Cr" : [ 20 , 22 , 24 , 26 ], 'sale_q2 in Cr' : [ 11 , 13 , 15 , 17 ]}, columns = [ "cars" , "sale_q1 in Cr" , 'sale_q2 in Cr' ]) # group by cars based on the sum # of sales on quarter 1 and 2 grouped_data = data.groupby(by = "cars" ).agg( "sum" ) print (grouped_data) # use reset_index to flattened # the hierarchical dataframe. flat_data = grouped_data.reset_index() print (flat_data) |
Output:
How to flatten a hierarchical index in Pandas DataFrame columns?
In this article, we are going to see the flatten a hierarchical index in Pandas DataFrame columns. Hierarchical Index usually occurs as a result of groupby() aggregation functions. Flatten hierarchical index in Pandas, the aggregated function used will appear in the hierarchical index of the resulting dataframe.