How to Change Index Values in Pandas?
Last Updated :
13 Jun, 2025
Indexes in Pandas uniquely identify each row and are integers by default. In practice, meaningful labels such as student IDs or dates are often preferred. Custom index values improve clarity, simplify data access and support accurate alignment during operations like merges and joins. Below are different methods to change index values efficiently.
Using set_index()
This method replaces default numeric row labels with meaningful ones (e.g., student IDs). It doesn’t modify the original DataFrame unless inplace= True is used.
Example 1: In this example, we perform a temporary change by setting 'AID' as the index without modifying the original DataFrame. This is the default behavior (inplace=False).
Python
import pandas as pd
df = pd.DataFrame({
'AID': ['AB101', 'AB102', 'AB103', 'AB104', 'AB105'],
'SID': ['21GFG1', '21GFG2', '21GFG3', '21GFG4', '21GFG5'],
'Name': ['Akhil', 'Mahesh Babu', 'Warner', 'Virat', 'ABD'],
'Ht': [5.9, 6.2, 5.6, 5.8, 5.10]
})
df_temp = df.set_index('AID')
print(df_temp)
print("\nOriginal DataFrame:\n", df)
Output
Using set_index()Explanation: df.set_index('AID') sets 'AID' as the row index without altering the original DataFrame unless inplace=True is used. Assigning it to df_temp creates a new DataFrame with 'AID' as the index.
Example 2: In this example, we perform a permanent change by setting 'Student_id' as the index using inplace=True. This directly modifies the original DataFrame.
Python
import pandas as pd
df = pd.DataFrame({
'Admission_id': ['AB101', 'AB102', 'AB103', 'AB104', 'AB105'],
'Student_id': ['21GFG1', '21GFG2', '21GFG3', '21GFG4', '21GFG5'],
'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner', 'Virat', 'ABD'],
'Height': [5.9, 6.2, 5.6, 5.8, 5.10]
})
df.set_index('Student_id', inplace=True)
print(df)
Output
Using set_index()Explanation: Here, inplace=True directly modifies df by setting 'Student_id' as the new index.
Using rename()
rename() updates specific index labels using a dictionary of old-to-new values. It’s useful for readability without altering the full index structure.
Python
import pandas as pd
df = pd.DataFrame({
'Admission_id': ['AB101', 'AB102', 'AB103', 'AB104', 'AB105'],
'Student_id': ['21GFG1', '21GFG2', '21GFG3', '21GFG4', '21GFG5'],
'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner', 'Virat', 'ABD'],
'Height': [5.9, 6.2, 5.6, 5.8, 5.10]
})
# Set 'Student_id' as index
df.set_index('Student_id', inplace=True)
# Rename specific index labels
df.rename(index={'21GFG1': 'GFG1', '21GFG2': 'GFG2'}, inplace=True)
print(df)
Output
Using rename()Explanation: Only the specified index values '21GFG1' and '21GFG2' are renamed to 'GFG1' and 'GFG2'. The rest remain unchanged.
Using index=[...]
df.index = [...] manually sets a new index by assigning a list of labels. It fully replaces the existing index and must match the number of rows.
Python
import pandas as pd
df = pd.DataFrame({
'Admission_id': ['AB101', 'AB102', 'AB103', 'AB104', 'AB105'],
'Student_id': ['21GFG1', '21GFG2', '21GFG3', '21GFG4', '21GFG5'],
'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner', 'Virat', 'ABD'],
'Height': [5.9, 6.2, 5.6, 5.8, 5.10]
})
df.index = ['S1', 'S2', 'S3', 'S4', 'S5']
print(df)
Output
Using indexExplanation: Here, we manually overwrite the entire index with a new list. The number of new labels must match the number of rows in the DataFrame.
Using reset_index()
reset_index() restores the default numeric index by moving the current index back to a regular column. Use inplace=True to apply changes directly.
Python
import pandas as pd
df = pd.DataFrame({
'AdmID': ['AB101', 'AB102', 'AB103', 'AB104', 'AB105'],
'StuID': ['21GFG1', '21GFG2', '21GFG3', '21GFG4', '21GFG5'],
'Name': ['Akhil', 'Mahesh Babu', 'Warner', 'Virat', 'ABD'],
'Ht': [5.9, 6.2, 5.6, 5.8, 5.10]
})
df.set_index('StuID', inplace=True)
df.reset_index(inplace=True)
print(df)
Output
Using reset_index()Explanation: df.set_index('StuID') sets 'StuID' as the index, while df.reset_index() restores it as a column, returning the DataFrame to its original structure.
Similar Reads
Python | Pandas Index.get_values() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.get_values() function returns the Index data as an numpy.ndarray. It retu
2 min read
How to Drop Index Column in Pandas? When working with Pandas DataFrames, it's common to reset or remove custom indexing, especially after filtering or modifying rows. Dropping the index is useful when:We no longer need a custom index.We want to restore default integer indexing (0, 1, 2, ...).We're preparing data for exports or transfo
2 min read
Python | Pandas Index.values Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.values attribute return an array representing the data in the given Index object. Syntax: Index.values Parameter : None Returns : an a
2 min read
How to Set Cell Value in Pandas DataFrame? In this article, we will discuss how to set cell values in Pandas DataFrame in Python. Method 1: Set value for a particular cell in pandas using dataframe.at This method is used to set the value of an existing value or set a new record. Python3 # import pandas module import pandas as pd # create a d
2 min read
How to use Hierarchical Indexes with Pandas ? The index is like an address, thatâs how any data point across the data frame or series can be accessed. Rows and columns both have indexes, rows indices are called index and for columns, it's general column names. Hierarchical Indexes Hierarchical Indexes are also known as multi-indexing is settin
3 min read
Python | Pandas Index.sort_values() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.sort_values() function is used to sort the index values. The function ret
2 min read