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

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.

Similar Reads

Using reset_index() function

Pandas provide a function called reset_index() to flatten the hierarchical index created due to the groupby aggregation function in Python....

Using as_index() function

...

Flattening hierarchical index in pandas dataframe using groupby

Pandas provide a function called as_index() which is specified by a boolean value. The as_index() functions groups the dataframe by the specified aggregate function and if  as_index() value is False, the resulting dataframe is flattened....

Flattening hierarchical index using to_records() function

...

Flattening hierarchical columns using join() and rstrip()

Whenever we use the groupby function on a single column with multiple aggregation functions we get multiple hierarchical indexes based on the aggregation type. In such cases, the hierarchical index has to be flattened at both levels....