How to Change Index Values in Pandas?
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).
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

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.
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

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.
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

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.
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

Explanation: 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.
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

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.