Open In App

How to reset index after Groupby pandas?

Last Updated : 29 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In pandas, groupby() is used to group data based on specific criteria, allowing for operations like aggregation, transformation and filtering. However, after applying groupby(), the resulting DataFrame often has a MultiIndex or a non-sequential index, which can make data handling more complex. Resetting the index after groupby() helps convert the grouped data back into a standard DataFrame format, making it easier to read, analyze and merge with other datasets. Let’s explore some methods to reset the index.

Using reset_index

reset_index() method restores the index of the grouped DataFrame to its default integer-based index, ensuring that the grouped column values are explicitly available as normal columns. Example:

Python
import pandas as pd
import numpy as np

df = pd.DataFrame({'Subject': ['Physics', 'Chemistry', 'Maths'],
                   'Marks': [4, 8, 5]})

# Grouping by 'Subject' and calculating mean
res = df.groupby(['Subject']).mean()
print(res)

# Reset index
res = res.reset_index()
print(res)

Output:

Output7890

Basic Resetting of Index

Explanation: This code creates a DataFrame, groups it by ‘Subject’, computes the mean of ‘Marks’ and resets the index while maintaining original values.

Example 2: Resetting Index After Grouping with Multiple Columns

Python
import pandas as pd
import numpy as np

df = pd.DataFrame({'Subject': ['B', 'C', 'A', 'D', 'C', 'B', 'A'],
                    'Marks': [4, 8, 5, 9, 8, 1, 0]})

# Grouping by 'Subject' and calculating mean
res = df.groupby(['Subject']).mean()
print(res)

# Reset index
res = res.reset_index()
print(res)

Output:

Output

Resetting Index After Grouping with Multiple Columns

Explanation: This code creates a DataFrame with ‘Subject’ and ‘Marks’, groups it by ‘Subject’, computes the mean of ‘Marks’ for each unique subject and resets the index to return a structured DataFrame.

Using as_index=False in groupby

Instead of explicitly calling reset_index(), you can pass as_index=False in groupby() to prevent pandas from setting the grouped column as the index.

Python
import pandas as pd

data = {
    'Category': ['A', 'B', 'A', 'B', 'A', 'B'],
    'Values': [10, 20, 30, 40, 50, 60]
}
df = pd.DataFrame(data)
print(df)

# Grouping without setting index
res = df.groupby('Category', as_index=False).sum()
print(res)

Output
  Category  Values
0        A      10
1        B      20
2        A      30
3        B      40
4        A      50
5        B      60
  Category  Values
0        A      90
1        B     120

Explanation: This code creates a DataFrame with ‘Category’ and ‘Values’, groups it by ‘Category’, calculates the sum of ‘Values’ for each category, and retains ‘Category’ as a column instead of setting it as an index.

Using reset_index(drop=True)

If you don’t need the grouped column to be retained, you can use drop=True inside reset_index(). This permanently removes the old index and returns a DataFrame with a default integer index.

Python
import pandas as pd

data = {
    'Category': ['A', 'B', 'A', 'B', 'A', 'B'],
    'Values': [10, 20, 30, 40, 50, 60]
}
df = pd.DataFrame(data)

# Reset index and drop the old index
res = df.groupby('Category').sum().reset_index(drop=True)
print(res)

Output
   Values
0      90
1     120

Explanation: This code creates a DataFrame, groups it by ‘Category’, sums the ‘Values’ for each category, resets the index while dropping the old index, and prints the final DataFrame without the original indexing.



Next Article
Practice Tags :

Similar Reads