{"id":6781,"date":"2021-05-24T08:48:29","date_gmt":"2021-05-24T03:18:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=6781"},"modified":"2021-11-22T18:40:45","modified_gmt":"2021-11-22T13:10:45","slug":"python-pandas-replace-or-change-column-row-index-names-in-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-pandas-replace-or-change-column-row-index-names-in-dataframe\/","title":{"rendered":"Python Pandas : Replace or change Column & Row index names in DataFrame"},"content":{"rendered":"
In this article we will discuss<\/p>\n
First, create an object with a database name for student records i.e.<\/p>\n
import pandas as pd\r\nstudents_record = [ ('Amit', 27, 'Kolkata') ,\r\n ('Mini', 24, 'Chennai' ) ,\r\n ('Nira', 34, 'Mumbai') ]\r\n# By creating a DataFrame object\r\ndo = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) \r\nprint(do)\r\n<\/pre>\nOutput :\r\n Name\u00a0 Age\u00a0\u00a0\u00a0\u00a0 City\r\nx\u00a0 Amit\u00a0\u00a0 27\u00a0 Kolkata\r\ny\u00a0 Mini\u00a0\u00a0 24\u00a0 Chennai\r\nz\u00a0 Nira\u00a0\u00a0 34\u00a0\u00a0 Mumbai<\/pre>\nChange Column Names in DataFrame :<\/h3>\n
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.<\/p>\n
import pandas as pd\r\nstudents_record = [ ('Amit', 27, 'Kolkata') ,\r\n ('Mini', 24, 'Chennai' ) ,\r\n ('Nira', 34, 'Mumbai') ]\r\n# By creating a DataFrame object\r\ndo = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) \r\n\r\n\r\n# By getting ndArray of all column names \r\ncolumn_Name_Arr = do.columns.values\r\nprint(column_Name_Arr)\r\n<\/pre>\nOutput :\r\n['Name' 'Age' 'City']<\/pre>\nAny 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.<\/p>\n
import pandas as pd\r\nstudents_record = [ ('Amit', 27, 'Kolkata') ,\r\n ('Mini', 24, 'Chennai' ) ,\r\n ('Nira', 34, 'Mumbai') ]\r\n# By creating a DataFrame object\r\ndo = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) \r\n\r\n\r\n# By getting ndArray of all column names \r\ncolumn_Name_Arr = do.columns.values\r\n# By Modifying a Column Name\r\ncolumn_Name_Arr[0] = 'Name_Vr'\r\nprint(column_Name_Arr)\r\n<\/pre>\nOutput :\r\n['Name_Vr' 'Age' 'City']<\/pre>\nChange Row Index in DataFrame<\/h2>\n
The content of the data items is as follows,<\/p>\n
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<\/code><\/p>\n
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 \u2018j\u2019.<\/p>\n
This change will be reflected in the linked DataFrame object again. Now the content of the DataFrame object is,<\/p>\n
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.<\/p>\n
The whole activities of the program is given below.<\/p>\n
import pandas as pd\r\nstudents_record = [ ('Amit', 27, 'Kolkata') ,\r\n ('Mini', 24, 'Chennai' ) ,\r\n ('Nira', 34, 'Mumbai') ]\r\n# By creating a DataFrame object\r\ndo = pd.DataFrame(students_record, columns = ['Name' , 'Age', 'City'], index=['x', 'y', 'z']) \r\n\r\n\r\n# For getting a list of all the column names \r\nindex_Name_Arr = do.index.values\r\nprint(index_Name_Arr)\r\n\r\n\r\n#For Modifying a Row Index Name\r\nindex_Name_Arr[0] = 'j'\r\nprint(index_Name_Arr)\r\n\r\n\r\n#For getting a copy list of all the column names \r\nindex_Names = list(do.index.values)\r\nprint(index_Names)\r\nprint(do)\r\n<\/pre>\nOutput :\r\n['x' 'y' 'z']\r\n['j' 'y' 'z']\r\n['j', 'y', 'z']\r\n Name\u00a0 Age\u00a0\u00a0\u00a0\u00a0 City\r\nj\u00a0 Amit\u00a0\u00a0 27\u00a0 Kolkata\r\ny\u00a0 Mini\u00a0\u00a0 24\u00a0 Chennai\r\nz\u00a0 Nira\u00a0\u00a0 34\u00a0\u00a0 Mumbai<\/pre>\nWant to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n
Read more Articles on Python Data Analysis Using Padas \u2013 Modify a Dataframe<\/strong><\/p>\n
\n
- pandas.apply(): Apply a function to each row\/column in Dataframe<\/a><\/li>\n
- Pandas: Sort rows or columns in Dataframe based on values using Dataframe.sort_values()<\/a><\/li>\n
- Apply a function to single or selected columns or rows in Dataframe<\/a><\/li>\n
- Sort a DataFrame based on column names or row index labels using Dataframe.sort_index() in Pandas<\/a><\/li>\n
- Change data type of single or multiple columns of Dataframe in Python<\/a><\/li>\n
- Convert Dataframe column type from string to date time<\/a><\/li>\n
- Convert Dataframe column into to the Index of Dataframe<\/a><\/li>\n
- Convert Dataframe indexes into columns<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"
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’ …<\/p>\n