In this article, we will discuss multiple ways to reset index in a pandas DataFrame.
Table of Contents
Preparing Dataset
To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.
import pandas as pd import numpy as np # List of Tuples employees= [('Shubham', 'Data Scientist', 'Tech', 5), ('Riti', 'Data Engineer', 'Tech' , 7), ('Shanky', 'Program Manager', 'PMO' , 2), ('Shreya', 'Graphic Designer', 'Design' , 2), ('Aadi', 'Backend Developer', 'Tech', 11), ('Sim', 'Data Engineer', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Designation', 'Team', 'Experience'], index = [10, 123, 33, 40, 20, 11]) print(df)
Contents of the created dataframe are,
Name Designation Team Experience 10 Shubham Data Scientist Tech 5 123 Riti Data Engineer Tech 7 33 Shanky Program Manager PMO 2 40 Shreya Graphic Designer Design 2 20 Aadi Backend Developer Tech 11 11 Sim Data Engineer Tech 4
As observed, the index in the above DataFrame is all over the place. Let’s explore multiple ways to reset it.
Frequently Asked:
Reset index of DataFrame using the index property
Every DataFrame has an index property that can be used to get/set the index of the DataFrame. Let’s understand by resetting the index of the above DataFrame.
# resetting the index df.index = range(len(df)) print (df)
Output
Name Designation Team Experience 0 Shubham Data Scientist Tech 5 1 Riti Data Engineer Tech 7 2 Shanky Program Manager PMO 2 3 Shreya Graphic Designer Design 2 4 Aadi Backend Developer Tech 11 5 Sim Data Engineer Tech 4
Here, we have set the DataFrame index as the range value (from 0 to DataFrame length). Therefore, our DataFrame now contains index values 0 to 5.
Reset index of DataFrame using reset_index() function
The most commonly used function to reset index values is the pandas.DataFrame.reset_index() function. It is the most simplest function without any complications. Let’s try to again reset the index of the original DataFrame.
Best Resources to Learn Python:
# reset index by dropping the current index df.reset_index(drop=True, inplace=True) print (df)
Output
Name Designation Team Experience 0 Shubham Data Scientist Tech 5 1 Riti Data Engineer Tech 7 2 Shanky Program Manager PMO 2 3 Shreya Graphic Designer Design 2 4 Aadi Backend Developer Tech 11 5 Sim Data Engineer Tech 4
Here you go, we have reset the DataFrame indexes. The two arguments used in the reset_index function are –
1. drop (default: False) – True means the current index column will be dropped, if False, it will create another column name “index” containing the original index values
2. inplace (default: False) – True means to store the changes in the DataFrame, if False, it will just print the changes but will not store them in the original DataFrame.
Reset index of DataFrame using set_index() function
The DataFrame.set_index() is another function to set the index of the DataFrame. The advantage of using this function is that it allows you to simply set any column as the index as well. Let’s try to set the index with the “Name” column values of the above DataFrame.
# Set column "Name" as the index of DataFrame df.set_index(['Name'], inplace=True) print (df)
Output
Designation Team Experience Name Shubham Data Scientist Tech 5 Riti Data Engineer Tech 7 Shanky Program Manager PMO 2 Shreya Graphic Designer Design 2 Aadi Backend Developer Tech 11 Sim Data Engineer Tech 4
Here you go, we now have the “Name” column as the index.
Summary
In this article, we have discussed multiple ways to reset index in a pandas DataFrame. Thanks.
Leave a Reply
You must be logged in to post a comment.