Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

How to find the column names of a Pandas dataframe

Recall that a Pandas dataframe is just like a table or spreadsheet, composed of columns and rows which contain data about some subject matter. Each column denotes typically one attribute (or variable) and each row contains one instance or observation of all variables.

For instance, here is a simple Pandas dataframe where the rows denote months and columns denote various facets of months:

The output will be:

Note that the days (rows) are numbered from 0 to 1.

To obtain a list of column names of this (or any) Pandas dataframe, there are three possible approaches.

Pandas get list of column names from a dataframe

First, we can use list(dataframe). Second, we can use dataframe.columns.values.tolist(). Or finally, we can use list(dataframe.columns.values). Let us try each of these methods in turn.

Method 1: list(dataframe)

The first approach is the simplest and just uses the list constructor with the dataframe as input. Here’s how this will work with our example above:

The output will be:

Note that the output is a list containing the three column names of our “months” dataframe.

Method 2: dataframe.columns.values.tolist()

In the second approach, we extract the columns, then the values and finally convert that into a list using the tolist() method. Here is how that works:

The output is once again:

Method 3: list(dataframe.columns.values)

The third approach is a minor variant of the second approach in that instead of using the tolist() method to convert the final answer into a list, we use the list() constructor:

The output is still the same:

In summary, these are three different ways to extract the column names of a Pandas dataframe into a list. Any one of them will serve your purpose; which one is your favorite?

If you liked this blogpost, checkout our blogpost on Pandas daterange. Also learn how to do cross-tabs in Python Pandas.

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.