In this article we discuss how to get a list of column and row names of a DataFrame object in python pandas.

First of all, create a DataFrame object of students records i.e.

Content of the DataFrame object dfObj is,

Original DataFrame pointed by dfObj

Let’s see how to get list of all column and row names from this DataFrame object,

Get Column Names from a DataFrame object

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.

Contents of columnsNamesArr are,

We can also create a list object from this ndarray object i.e.

listOfColumnNames is a list that contains all the column names of a DataFrame object i.e.

Get Column name by Index / position in DataFrame

As df.column.values is a ndarray, so we can access it contents by index too. So, let’s get the name of column at index 2 i.e.

It returns,

Get Row Index Label Names from a DataFrame object

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 i.e.

We can also create a list object from this ndarray object i.e.

listOfRowIndexLabels is a list that contains all the row index labels of a DataFrame object i.e.

Get Row Index name by position in DataFrame

As df.index.values is a ndarray, so we can access it contents by position too. So, let’s get the name of column at position 2 i.e.

It returns,

Complete example is as follows,

Output:

 

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