Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - How to reset index after Groupby pandas?
When you perform a groupby operation in pandas, the grouped column becomes the index. To convert this index back to a regular column, use reset_index().
Import Required Library
First, import pandas ?
import pandas as pd
Creating Sample DataFrame
Let's create a DataFrame with car names and prices ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"],
"Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
})
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Car Reg_Price
0 Audi 1000
1 Lexus 1400
2 Audi 1100
3 Mercedes 900
4 Audi 1700
5 Lexus 1800
6 Mercedes 1300
7 Lexus 1150
8 Mercedes 1350
GroupBy Operation
Group by the "Car" column and calculate the mean price ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"],
"Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
})
# Group by Car and calculate mean
grouped_df = dataFrame.groupby("Car").mean()
print("After GroupBy (Car becomes index):")
print(grouped_df)
After GroupBy (Car becomes index):
Reg_Price
Car
Audi 1266.666667
Lexus 1450.000000
Mercedes 1183.333333
Reset Index After GroupBy
Use reset_index() to convert the index back to a column ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"],
"Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
})
# Group by Car and calculate mean
grouped_df = dataFrame.groupby("Car").mean()
# Reset index to make Car a regular column again
result_df = grouped_df.reset_index()
print("After reset_index():")
print(result_df)
After reset_index():
Car Reg_Price
0 Audi 1266.666667
1 Lexus 1450.000000
2 Mercedes 1183.333333
Alternative: Using drop Parameter
You can also use reset_index(drop=True) to drop the old index completely ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ["Audi", "Lexus", "Audi", "Mercedes"],
"Price": [1000, 1400, 1100, 900],
"Year": [2020, 2021, 2019, 2022]
})
# Group by Car and get first row of each group
grouped_df = dataFrame.groupby("Car").first()
# Reset index and drop the old index
result_df = grouped_df.reset_index(drop=False) # Keep grouped column
print(result_df)
Car Price Year
0 Audi 1000 2020
1 Lexus 1400 2021
2 Mercedes 900 2022
Conclusion
Use reset_index() after groupby() to convert the grouped column back to a regular column. This is essential when you need the grouped column for further operations or display purposes.
Advertisements
