Get Column Names of pandas DataFrame as List in Python (2 Examples)

 

In this tutorial you’ll learn how to create a list of column names in Python programming.

Table of contents:

You’re here for the answer, so let’s get straight to the exemplifying Python syntax.

 

Creation of Example Data

Let’s first create some example data. For this, we have to load the pandas library to Python:

import pandas as pd                                      # Load pandas

As the next step, we can create a pandas DataFrame as shown below:

data = pd.DataFrame({"x1":["xxx", "yyy", "abc", "xxx"],  # Create example data
                     "x2":range(1, 5),
                     "x3":["a", "d", "a", "b"],
                     "x4":[10, 30, 50, 70]})
print(data)                                              # Print example data
#     x1  x2 x3  x4
# 0  xxx   1  a  10
# 1  yyy   2  d  30
# 2  abc   3  a  50
# 3  xxx   4  b  70

Have a look at the previous output of the console. As you can see, we have created a pandas DataFrame with four rows and four columns.

In the following examples, I’ll explain how to extract the header names of our DataFrame columns and how to store these names in a list.

Let’s do this!

 

Example 1: Create List of Column Names Using list() Function

In this example, I’ll illustrate how to apply the list() function to create a Python list containing all variables names of a pandas DataFrame.

Have a look at the following Python syntax and its output:

my_column_names_1 = list(data)                           # Apply list function
print(my_column_names_1)
# ['x1', 'x2', 'x3', 'x4']

As you can see, we have created a list object called my_column_names_1 that consists of the character strings x1, x2, x3, and x4 (i.e. the column names of our DataFrame).

 

Example 2: Create List of Column Names Using columns Attribute & tolist() Function

The Python syntax below illustrates an alternative to the list() function that I have explained in the previous example.

In this example, we’ll use the columns attribute and the tolist() function to get a list of all column names of a pandas DataFrame:

my_column_names_2 = data.columns.tolist()                # Apply tolist function
print(my_column_names_2)
# ['x1', 'x2', 'x3', 'x4']

The previous output shows exactly the same values as in the first example. However, this time we have used a different Python syntax to construct our list of variable names.

 

Video & Further Resources on this Topic

I have recently released a video on my YouTube channel, which shows the Python syntax of this article. You can find the video below:

 

 

In case you need further information on this topic, I recommend watching the following video on the YouTube channel of Erik Marsja. In the video, he explains six different ways on how to get a list of column names in Python:

 

 

In addition, you may read the related tutorials on my website:

 

To summarize: In this Python article you have learned how to get all variable names of a pandas DataFrame. Let me know in the comments section, in case you have any further comments or questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top