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.

Contents of DataFrame objects are as follows,

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.

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.

This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,

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.

or

Now modify the list,

This modification will not be reflected in original DataFrame object and DataFrame object will remain same i.e.

Change Row Index in DataFrame

Contents of dataframe objects  is as follows,

To get the list of all row index names from a dataFrame object, use index attribute instead of columns i.e. df.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.

This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,

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.

 

Complete example is as follows :

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.