How to Get a List of All Column Names in Pandas DataFrame?

The Pandas module in Python is used for data manipulation and handling structured data. You must know the names of the columns in a Pandas DataFrame in order to select, filter, and manipulate the data. Python provides different methods for getting all column names in Pandas DataFrame.

In this blog, we will explain various methods to get/find a list of all column names in Pandas DataFrame.

Method 1: Using list() Function

The below code uses the “list()” function to get a list of all column names in pandas DataFrame:

Code: 

import pandas

data = {'Students': ['Joseph', 'Henry', 'Alex', 'Lily'],
        'Class': [11, 12, 5, 12], 'Marks': [50, 60, 99, 100]}
data_frame = pandas.DataFrame(data)
print(data_frame)
list_of_columns = list(data_frame)
print ('\nList of All Columns: ',list_of_columns)
print (type(list_of_columns))
  • Firstly, we import the pandas DataFrame.
  • Next, the DataFrame is created using the “pd.DataFrame()” function.
  • The “list()” function accepts the Pandas DataFrame as an argument and returns the list of all column names of the pandas DataFrame.

Output: 

The DataFrame and list of all columns of DataFrame have been displayed in the above snippet.

Method 2: Using df.columns.values.tolist() Function

The “df.columns.values.tolist()” function is used in the below code to get a list of all column names of DataFrame:

Code:

import pandas

data = {'Students': ['Joseph', 'Henry', 'Alex', 'Lily'],
        'Class': [11, 12, 5, 12], 'Marks': [50, 60, 99, 100]}
data_frame = pandas.DataFrame(data)
print(data_frame)
list_of_columns = data_frame.columns.values.tolist()
print ('\nList of All Columns: ',list_of_columns)
print (type(list_of_columns))
  • The “data_frame.columns.values.tolist()” function returns the column names of Pandas DataFrame that will be stored in a variable named “list_of_columns”.
  • The print() function is utilized to display the name of the columns of pandas DataFrame.

Output:

The list of all columns of the given DataFrame has been printed on the screen.

Method 3: Using df.keys()

The “df.keys()” function is used in the below code to get a list of all column names in Pandas:

Code:

import pandas

data = {'Students': ['Joseph', 'Henry', 'Alex', 'Lily'],
        'Class': [11, 12, 5, 12], 'Marks': [50, 60, 99, 100]}
data_frame = pandas.DataFrame(data)
print(data_frame)
list_of_columns = list(data_frame.keys())
print ('\nList of All Columns: ',list_of_columns)
print (type(list_of_columns))
  • The “list()” function accepts the “df.keys()” function as an argument.
  • The “df.keys()” function gets the column’s name and passes it to the “list()” function to convert it into a list.

Output:

The list of all columns has been displayed successfully.

Conclusion

To get a list of all column names in Pandas DataFrame the “list()”, “df.columns.values.tolist()”, and “df.keys()” functions are used in Python. The “list()” constructor/function is a simple and straightforward way to find a list of all column names in Pandas DataFrame. The Pandas module function named “df.columns.values.tolist()” can also be used to find the column names of Pandas DataFrame. This guide delivered a complete guide on how to find a list of all column names in Pandas DataFrame using appropriate examples.