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.

# List of tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

# Create DataFrame object from a list of tuples
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])

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.

# Get ndArray of all column names 
columnsNamesArr = dfObj.columns.values

Contents of columnsNamesArr are,
array(['Name', 'Age', 'City', 'Country'], dtype=object)

We can also create a list object from this ndarray object i.e.
listOfColumnNames = list(columnsNamesArr)

listOfColumnNames is a list that contains all the column names of a DataFrame object i.e.
['Name', 'Age', 'City', 'Country']

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.

dfObj.columns.values[2]

It returns,
'City'

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

# get a list of all the column names 
indexNamesArr = dfObj.index.values

It returns an ndarray of all row indexes in dataframe i.e.
array(['a', 'b', 'c', 'd', 'e', 'f'], dtype=object)

We can also create a list object from this ndarray object i.e.
listOfRowIndexLabels = list(indexNamesArr)

listOfRowIndexLabels is a list that contains all the row index labels of a DataFrame object i.e.
['a', 'b', 'c', 'd', 'e', 'f']

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.

dfObj.index.values[2]

It returns,
'c'

Complete example is as follows,
import pandas as pd

def main():
    
    # List of Tuples
    students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]
    
    # Create DataFrame object
    dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])
    
    print("Original Dataframe" , dfObj, sep='\n')
    
    '''
    Get Column Names
    '''
    
    # Get ndArray of all column names 
    columnsNamesArr = dfObj.columns.values

    # convert ndarray to list
    listOfColumnNames = list(columnsNamesArr)
    
    print("List Of Column Names" , listOfColumnNames, sep='\n')

    print("Column name at index 2" , dfObj.columns.values[2], sep='\n')
    
    '''
    Get Row Names
    '''
    # get a list of all the column names 
    indexNamesArr = dfObj.index.values
    
    # convert ndarray to list
    listOfRowIndexLabels = list(indexNamesArr)
    
    print("List Of Row Index Labels" , listOfRowIndexLabels, sep='\n')
    
    print("Row Index Label at index 2" , dfObj.index.values[2], sep='\n')
    
    
if __name__ == '__main__':
    main()



Output:
Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
List Of Column Names
['Name', 'Age', 'City', 'Country']
Column name at index 2
City
List Of Row Index Labels
['a', 'b', 'c', 'd', 'e', 'f']
Row Index Label at index 2
c