How to Change Index Values in Pandas? - GeeksforGeeks

21 Feb, 2022
Index is used to uniquely identify a row in Pandas DataFrame. It is nothing but a label to a row. If we didn’t specify index values to the DataFrame while creation then it will take default values i.e. numbers starting from 0 to n-1 where n indicates a number of rows.
Let’s create a dataframe
Example:
Python3
# import necessary packagesimport pandas as pd # create a dataframeStudents = 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]})# display dataframeStudents
Output:
To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes.
Syntax
DataFrameName.set_index(“column_name_to_setas_Index”,inplace=True/False)
where,
inplace parameter accepts True or False, which specifies that change in index is permanent or temporary.
True indicates that change is Permanent.
False indicates that the change is Temporary.
Example 1:
Changing the index temporarily by specifying inplace=False (or) we can make it without specifying inplace parameter because by default the inplace value is false.
Python3
# import necessary packagesimport pandas as pd # create a dataframeStudents = 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]}) # setting admission id as index but temporarilyStudents.set_index("Admission_id")
Output:
But when we displayed the data in DataFrame but it still remains as previous because the operation performed was not saved as it is a temporary operation.
Explanation– As we didn’t specify inplace parameter in set_index method, by default it is taken as false and considered as a temporary operation.
Example 2 :
Changing the index permanently by specifying inplace=True in set_index method.
Example:
Python3
# import necessary packagesimport pandas as pd # create a dataframeStudents = 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]}) # setting student id as index but permanentlyStudents.set_index("Student_id", inplace=True) # display dataframeStudents
Output:
 
Example 3  :
When we want to retrieve only particular columns instead of all columns follow the below code
Python3
# import necessary packagesimport pandas as pd # create a dataframeStudents = 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]}) # setting student id as index but permanentlyStudents.set_index("Student_id", inplace=True) # display dataframe with required columnsStudents[["Student_Name", "Height"]]
Output
kk773572498
as5853535
Picked
Python pandas-indexing
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()