In this article we will discuss how to change column names or Row Index names in DataFrame object.
First of all, create a dataframe object of students records i.e.
students = [ ('jack', 34, 'Sydeny') , ('Riti', 30, 'Delhi' ) , ('Aadi', 16, 'New York') ] # Create a DataFrame object dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City'], index=['a', 'b', 'c'])
Contents of DataFrame objects are as follows,
Name Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
Change Column Names in DataFrame
DataFrame object has an Attribute columns that is basically an Index object and contains column Labels of Dataframe. We can get the ndarray of column names from this Index object i.e.
# Get ndArray of all column names columnsNamesArr = dfObj.columns.values
Any modification in this ndArray (df.column.values) will modify the actual DataFrame. For example let’s change the name of column at index 0 i.e.
# Get ndArray of all column names columnsNamesArr = dfObj.columns.values # Modify a Column Name columnsNamesArr[0] = 'Test'
This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,
Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
But we convert it to list before modifying then changes will not be reflected in original DataFrame object.
For example create a list of Column Names of DataFrame i.e.
# get a copy list of all the column names columnsNames = list(dfObj.columns.values)
or
columnsNames = list(dfObj)
Now modify the list,
# Modify Column Name columnsNames[0] = 'Test_Name'
This modification will not be reflected in original DataFrame object and DataFrame object will remain same i.e.
Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
Change Row Index in DataFrame
Contents of dataframe objects is as follows,
Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
To get the list of all row index names from a dataFrame object, use index attribute instead of columns i.e. df.index.values
# get a list of all the column names indexNamesArr = dfObj.index.values
It returns an ndarray of all row indexes in dataframe. Any modification in this ndArray (df.index.values) will modify the actual DataFrame. For example let’s change the name of row index at position 0 i.e.
# Modify a Row Index Name indexNamesArr[0] = 'P'
This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,
Test Age City P jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
But if we convert it to list before modifying then changes will not be reflected in original DataFrame object. For example create a copy list of Row Index Names of DataFrame i.e.
# get a copy list of all the column names indexNames = list(dfObj.index.values)
Complete example is as follows :
import pandas as pd def main(): students = [ ('jack', 34, 'Sydeny') , ('Riti', 30, 'Delhi' ) , ('Aadi', 16, 'New York') ] # Create a DataFrame object dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City'], index=['a', 'b', 'c']) print("Original DataFrame : " , dfObj, sep="\n"); ''' Get All Columns Names in DataFrame ''' # Get ndArray of all column names columnsNamesArr = dfObj.columns.values print("Column Names : " , columnsNamesArr) ''' Get Columns Name by Index/position in DataFrame ''' print("Column Names at index 2: " , dfObj.columns.values[2]) ''' Pandas : Modify Column Name in DataFrame ''' # Modify a Column Name columnsNamesArr[0] = 'Test' print("Modified Column Names : " , columnsNamesArr) print("Modified DataFrame : ") print(dfObj) ''' Get Copy of all Columns Names in DataFrame ''' # get a copy list of all the column names columnsNames = list(dfObj.columns.values) print("Column Names : " , columnsNames) # Modify Column Name columnsNames[0] = 'Test_Name' print("Modified Column Names : " , columnsNames) print("DataFrame : ") print(dfObj) ''' Get List of All Index Names in DataFrame ''' # get a list of all the column names indexNamesArr = dfObj.index.values print("All Index Names : " , indexNamesArr) ''' Get Row Index Names in DataFrame by Poisition ''' print("Row Index Names at position 2: " , dfObj.index.values[2]) ''' Pandas : Modify Row Index Name in DataFrame ''' # Modify a Row Index Name indexNamesArr[0] = 'P' print("Modified Row Index Names : " , indexNamesArr) print("Modified DataFrame : ", dfObj, sep='\n') ''' Get Copy of all Row Index Names in DataFrame ''' # get a copy list of all the column names indexNames = list(dfObj.index.values) print("All Row Index Names : " , indexNames) if __name__ == '__main__': main()
Output:
Original DataFrame : Name Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York Column Names : ['Name' 'Age' 'City'] Column Names at index 2: City Modified Column Names : ['Test' 'Age' 'City'] Modified DataFrame : Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York Column Names : ['Test', 'Age', 'City'] Modified Column Names : ['Test_Name', 'Age', 'City'] DataFrame : Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York All Index Names : ['a' 'b' 'c'] Row Index Names at position 2: c Modified Row Index Names : ['P' 'b' 'c'] Modified DataFrame : Test Age City P jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York All Row Index Names : ['P', 'b', 'c']
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.