Skip to Content

Get Column Names as List in Pandas DataFrame

While working with pandas dataframes it may happen that you require a list of all the column names present in a dataframe. You can use df.columns to get the column names but it returns them as an Index object. In this tutorial, we’ll show some of the different ways in which you can get the column names of a dataframe as a list which gives you more flexibility for further usage.

How to get the list of column names of a Pandas dataframe?

You can use the list() function in Python to get the column names of a Pandas dataframe as a list. Pass the dataframe as an argument to the list() function. The following is the syntax –

# Method 1
list(df)

There are alternate methods as well to get the column names as a list. For example, you can use df.columns.values.tolist() or a list comprehension. The following is the syntax of these methods –

# Method 2
df.columns.values.tolist()

# Method 3 - list comprehension
[col for col in df]

Examples

Let’s now look at some examples to get the column names of a dataframe as a list.

First, let’s create a sample dataframe that we’ll be using throughout this tutorial.

import pandas as pd

data = {
    "Name": ["Google, LLC", "Microsoft Corporation", "Tesla, Inc."],
    "Symbol": ["GOOG", "MSFT", "TSLA"],
    "Shares": [100, 50, 80],
}

# create dataframe
df = pd.DataFrame(data)
# display dataframe
df

pandas dataframe with column names - Name, Symbol, and Shares

Here, df is a dataframe storing information on a sample portfolio of US companies with their Name, Stock Symbol, and the number of shares in the portfolio.

Let’s see what we get accessing the columns attribute of the dataframe df.

print(df.columns)

Output:

Index(['Name', 'Symbol', 'Shares'], dtype='object')

We see that an Index object with the column names is returned. It would be convenient if we could have it as a simple list.

Pass the dataframe to the list() function to get the list of column names.

print(list(df))

Output:

['Name', 'Symbol', 'Shares']

Alternatively, you can also use the df.columns.values.tolist() method. We know that df.columns returns an Index, now .values on it returns an array and it has a helper function .tolist() to return a list.

print(df.columns.values.tolist())

Output:

['Name', 'Symbol', 'Shares']

We get the same result as above.

You can also get the columns as a list using list comprehension.

print([col for col in df])

Output:

['Name', 'Symbol', 'Shares']

Now let’s see which of the three methods shown above is the fastest. For this, we’ll be using the %timeit magic function.

%timeit list(df)
%timeit df.columns.values.tolist()
%timeit [col for col in df]

Output:

4.78 µs ± 592 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
1.03 µs ± 113 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
4.31 µs ± 435 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

We find that df.columns.values.tolist() is the fastest of the three. Also, note that the list() and the list comprehension methods are comparable to each other and differences might occur when working with large dataframes.

There are other ways as well to get column names as a list for a pandas dataframe but they may be more or less an extension or variation of the above three methods. For more, refer to this thread on Stack Overflow.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.