Hello readers! In this tutorial, we are going to discuss how to reset index of a DataFrame object using reset_index() and concat() functions. We will also discuss the different scenarios where we need to reset the index of a pandas DataFrame.
Also read: Indexing a dataframe in Python
Syntax of the reset_index() function in pandas
In Python, we can reset the index of a pandas DataFrame object using the reset_index()
function of the pandas DataFrame class. The reset_index()
function resets the index of a pandas DataFrame to the Pandas default index by default and returns either a pandas DataFrame object with a new index or None
value. If the Pandas DataFrame has more than one level of index, then this function can remove one or more levels. Let’s quickly understand the syntax of the reset_index()
function.
# Syntax of the reset_index() function in pandas
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
Mostly, we will be using only the two parameters drop and inplace rest of the parameters are used less frequently.
- drop: It tries not to insert the index into the DataFrame columns. It resets the index of the concerned pandas DataFrame to the default integer index. It takes a boolean value i.e. True or False which is by default False.
- inplace: It does not create a new pandas DataFrame object rather it does the modification i.e. reset the DataFrame’s index in place. It also receives a boolean value which is by default False.
Reset Index of a DataFrame using the reset_index() function
In Python, we need to reset the index of the pandas DataFrame object in the following scenarios:
1. When rows are inserted in the DataFrame
If we add few rows in our original DataFrame object, the new row index starts at 0. Here, we can apply the reset_index()
function to reset index of a DataFrame. Take a look at the below demonstration
# Case-1
# When some rows are inserted in the DataFrame
# Import pandas
import pandas as pd
# Create a DataFrame object
# Using DataFrame() function
df1 = pd.DataFrame({"Date": ['11/05/21', '15/06/21', '17/07/21'],
"Item": ['Television', 'Speaker', 'Monitor'],
"Sales": [200, 300, 115]})
print("Original DataFrame:\n")
print(df1)
# Create an another DataFrame
df2 = pd.DataFrame({"Date": ['04/05/20', '29/07/20', '30/08/20'],
"Item": ['Mixer', 'Bulb', 'Cooler'],
"Sales": [803, 178, 157]})
# Add the rows of DataFrame (df2) to the DataFrame (df1)
# Using the concat() function
df = pd.concat([df1, df2])
print("\nDataFrame after inserting some rows:\n")
print(df)
# Reset the index of the final DataFrame
# Using reset_index() function
df.reset_index(drop = True, inplace = True)
print("\nDataFrame after the resetting the index:\n")
print(df)
Output:
Original DataFrame:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Monitor 115
DataFrame after inserting some rows:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Monitor 115
0 04/05/20 Mixer 803
1 29/07/20 Bulb 178
2 30/08/20 Cooler 157
DataFrame after the resetting the index:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Monitor 115
3 04/05/20 Mixer 803
4 29/07/20 Bulb 178
5 30/08/20 Cooler 157
2. When rows are deleted in the DataFrame
In this case, we first drop/delete few selected rows from our original DataFrame object where the index gets messed up. We then apply the reset_index()
function on the final DataFrame to recount the values. Let’s see the Python Code to implement this case.
# Case-2
# When few rows from DataFrame are deleted
# Import pandas Python module
import pandas as pd
# Create a DataFrame object
# Using DataFrame() function
df = pd.DataFrame({"Date": ['11/05/21', '15/06/21', '17/07/21', '19/11/20', '21/12/20'],
"Item": ['Television', 'Speaker', 'Desktop', 'Dish-Washer', 'Mobile'],
"Sales": [200, 300, 115, 303, 130]})
print("Original DataFrame:\n")
print(df)
# Delete few rows of the DataFrame (df)
# Using drop() function
df = df.drop(labels = [0, 3], axis = 0)
print("\nDataFrame after deleting few rows:\n")
print(df)
# Reset the index of the final DataFrame
# Using reset_index() function
df.reset_index(drop = True, inplace = True)
print("\nDataFrame after the resetting the index:\n")
print(df)
Output:
Original DataFrame:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
3 19/11/20 Dish-Washer 303
4 21/12/20 Mobile 130
DataFrame after deleting few rows:
Date Item Sales
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
4 21/12/20 Mobile 130
DataFrame after the resetting the index:
Date Item Sales
0 15/06/21 Speaker 300
1 17/07/21 Desktop 115
2 21/12/20 Mobile 130
3. When rows are sorted in the dataframe
In this case, we first sort the rows of our original DataFrame object according to one or multiple columns then apply the reset_index()
function on the final DataFrame object. Let’s see how to implement this case through Python Code.
# Case-3
# When rows of the DataFrame are sorted
# Import pandas Python module
import pandas as pd
# Create a DataFrame object
# Using DataFrame() function
df = pd.DataFrame({"Date": ['11/05/21', '15/06/21', '17/07/21', '19/11/20', '21/12/20'],
"Item": ['Television', 'Speaker', 'Desktop', 'Dish-Washer', 'Mobile'],
"Sales": [200, 300, 115, 303, 130]})
print("Original DataFrame:\n")
print(df)
# Sort the rows of the DataFrame (df)
# Using sort_values() function
df.sort_values(by = "Sales", inplace = True)
print("\nDataFrame after sorting the rows by Sales:\n")
print(df)
# Reset the index of the final DataFrame
# Using reset_index() function
df.reset_index(drop = True, inplace = True)
print("\nDataFrame after the resetting the index:\n")
print(df)
Output:
Original DataFrame:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
3 19/11/20 Dish-Washer 303
4 21/12/20 Mobile 130
DataFrame after sorting the rows by Sales:
Date Item Sales
2 17/07/21 Desktop 115
4 21/12/20 Mobile 130
0 11/05/21 Television 200
1 15/06/21 Speaker 300
3 19/11/20 Dish-Washer 303
DataFrame after the resetting the index:
Date Item Sales
0 17/07/21 Desktop 115
1 21/12/20 Mobile 130
2 11/05/21 Television 200
3 15/06/21 Speaker 300
4 19/11/20 Dish-Washer 303
4. When two data frames are appended
Again, it is a commonly used case where we have to reset the index of the pandas DataFrame object. In this case, we first append another DataFrame object to our original DataFrame object then apply the reset_index()
function on the final combined DataFrame object. Let’s write Python Code to implement this case.
# Case-4
# When two DataFrames are appended
# Import pandas Python module
import pandas as pd
# Create a DataFrame object
# Using DataFrame() function
df1 = pd.DataFrame({"Date": ['11/05/21', '15/06/21', '17/07/21'],
"Item": ['Television', 'Speaker', 'Desktop'],
"Sales": [200, 300, 115]})
print("Original DataFrame:\n")
print(df1)
# Create a new DataFrame
df2 = pd.DataFrame({"Date": ['19/11/20', '21/12/20'],
"Item": ['Dish-Washer', 'Mobile'],
"Sales": [403, 130]})
# Append the new DataFrame (df1) to the previous one (df2)
df = df1.append(df2)
print("\nDataFrame after appending the new DataFrame:\n")
print(df)
# Reset the index of the final DataFrame
# Using reset_index() function
df.reset_index(drop = True, inplace = True)
print("\nDataFrame after the resetting the index:\n")
print(df)
Output:
Original DataFrame:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
DataFrame after appending the new DataFrame:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
0 19/11/20 Dish-Washer 403
1 21/12/20 Mobile 130
DataFrame after the resetting the index:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
3 19/11/20 Dish-Washer 403
4 21/12/20 Mobile 130
Reset Index of a DataFrame using the concat() function
In Python, we can also reset the index of a pandas DataFrame object using the pandas concat()
function along with ignor_index
parameter. By default, the value of the ignore_index
parameter is False. In order to reset the index of the DataFrame, we have to set its value as True. Let’s implement this through Python code.
# Reset the index of DataFrame using concat() function
# Import pandas Python module
import pandas as pd
# Create a DataFrame object
# Using DataFrame() function
df1 = pd.DataFrame({"Date": ['11/05/21', '15/06/21', '17/07/21'],
"Item": ['Television', 'Speaker', 'Desktop'],
"Sales": [200, 300, 115]})
print("Original DataFrame:\n")
print(df1)
# Create a new DataFrame
df2 = pd.DataFrame({"Date": ['14/10/20', '19/11/20', '21/12/20'],
"Item": ['Oven', 'Toaster', 'Fan'],
"Sales": [803, 178, 157]})
# Concat the new DataFrame (df2) with the prevous one (df1)
# And reset the index of the DataFrame
# Using the concat() function with ignor_index parameter
df = pd.concat([df1, df2], ignore_index = True)
print("\nDataFrame after concatenation and index reset:\n")
print(df)
Output:
Original DataFrame:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
DataFrame after concatenation and index reset:
Date Item Sales
0 11/05/21 Television 200
1 15/06/21 Speaker 300
2 17/07/21 Desktop 115
3 14/10/20 Oven 803
4 19/11/20 Toaster 178
5 21/12/20 Fan 157
Conclusion
In this tutorial, we have learned how to use and when to use the pandas reset_index()
function to reset the index of the modified pandas DataFrame object. Hope you have understood the things discussed above and excited to perform these DataFrame operations on your own. Thanks for reading, stay tuned with us for more such resourceful articles related to Python.