Replacing or changing Column & Row index names in DataFrame
In this article we will discuss
- How to change column names or
- Row Index names in the DataFrame object.
First, create an object with a database name for student records i.e.
import pandas as pd students_record = [ ('Amit', 27, 'Kolkata') , ('Mini', 24, 'Chennai' ) , ('Nira', 34, 'Mumbai') ] # By creating a DataFrame object do = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) print(do)
Output : Name Age    City x Amit  27 Kolkata y Mini  24 Chennai z Nira  34  Mumbai
Change Column Names in DataFrame :
The DataFrame item contains Attribute columns which is the Index item and contains the Data Labels in the Dataframe column. We can find column name detection in this Index item i.e.
import pandas as pd students_record = [ ('Amit', 27, 'Kolkata') , ('Mini', 24, 'Chennai' ) , ('Nira', 34, 'Mumbai') ] # By creating a DataFrame object do = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) # By getting ndArray of all column names column_Name_Arr = do.columns.values print(column_Name_Arr)
Output : ['Name' 'Age' 'City']
Any modifications to this ndArray (df.column.values) will change the actual DataFrame. For example let’s change the column name to index 0 i.e.
import pandas as pd students_record = [ ('Amit', 27, 'Kolkata') , ('Mini', 24, 'Chennai' ) , ('Nira', 34, 'Mumbai') ] # By creating a DataFrame object do = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) # By getting ndArray of all column names column_Name_Arr = do.columns.values # By Modifying a Column Name column_Name_Arr[0] = 'Name_Vr' print(column_Name_Arr)
Output : ['Name_Vr' 'Age' 'City']
Change Row Index in DataFrame
The content of the data items is as follows,
To get a list of all the line references names from the dataFrame object, there use the attribute index instead of columns i.e. df.index.values
It returns the ndarray of all line references to the data file. Any modifications to this ndArray (df.index.values) will modify the actual DataFrame. For example let’s change the name of the line indicator to 0 i.e.replace with ‘j’.
This change will be reflected in the linked DataFrame object again. Now the content of the DataFrame object is,
But if we change it to the list before changing the changes it will not be visible in the original DataFrame object. For example create a list of copies of Row Index Names of DataFrame i.e.
The whole activities of the program is given below.
import pandas as pd students_record = [ ('Amit', 27, 'Kolkata') , ('Mini', 24, 'Chennai' ) , ('Nira', 34, 'Mumbai') ] # By creating a DataFrame object do = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) # For getting a list of all the column names index_Name_Arr = do.index.values print(index_Name_Arr) #For Modifying a Row Index Name index_Name_Arr[0] = 'j' print(index_Name_Arr) #For getting a copy list of all the column names index_Names = list(do.index.values) print(index_Names) print(do)
Output : ['x' 'y' 'z'] ['j' 'y' 'z'] ['j', 'y', 'z'] Name Age    City j Amit  27 Kolkata y Mini  24 Chennai z Nira  34  Mumbai
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Read more Articles on Python Data Analysis Using Padas – Modify a Dataframe
- pandas.apply(): Apply a function to each row/column in Dataframe
- Pandas: Sort rows or columns in Dataframe based on values using Dataframe.sort_values()
- Apply a function to single or selected columns or rows in Dataframe
- Sort a DataFrame based on column names or row index labels using Dataframe.sort_index() in Pandas
- Change data type of single or multiple columns of Dataframe in Python
- Convert Dataframe column type from string to date time
- Convert Dataframe column into to the Index of Dataframe
- Convert Dataframe indexes into columns