Get Column Names as List in Pandas DataFrame

While working pandas dataframes it may happen that you require a list 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 as a list which gives you more flexibility for further usage.

If you quickly want to know the syntax please refer to the following –

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

Before we proceed to examples and comparisons 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],

df = pd.DataFrame(data)

Sample dataframe with column names Name, Symbol, and Shares

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

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



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.



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



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

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.

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

print([col for col in df])


['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]


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 list(df) and list comprehension 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 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.