How to Get Column Names in Pandas (3 Methods)

Pandas provides three methods to get the column names of a dataframe. These include the columns attribute, df.columns.tolist() method, and the df.keys() method. The columns attribute returns the column names as an Index object. The df.columns.tolist() method returns the column names as a list. The df.keys() method returns the column names as a pandas.core.indexes.base.Index object. All three methods can be used to get the column names of a dataframe in Pandas.


You can use the following methods to get the column names in a pandas DataFrame:

Method 1: Get All Column Names

list(df)

Method 2: Get Column Names in Alphabetical Order

sorted(df)

Method 3: Get Column Names with Specific Data Type

list(df.select_dtypes(include=['int64', 'bool']))

The following examples show how to use each method with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'points': [18, 22, 19, 14, 14, 11],
                   'assists': [5, 7, 7, 9, 12, 9],
                   'playoffs': [True, False, False, True, True, True]})

#view DataFrame
print(df)

  team  points  assists  playoffs
0    A      18        5      True
1    B      22        7     False
2    C      19        7     False
3    D      14        9      True
4    E      14       12      True
5    F      11        9      True

Example 1: Get All Column Names

The easiest way to get all of the column names in a pandas DataFrame is to use list() as follows:

#get all column names
list(df)

['team', 'points', 'assists', 'playoffs']

The result is a list that contains all four column names from the pandas DataFrame.

Example 2: Get Column Names in Alphabetical Order

To get the column names in a pandas DataFrame in alphabetical order, you can use the sorted() function as follows:

#get column names in alphabetical order
sorted(df)

['assists', 'playoffs', 'points', 'team']

The result is a list that contains all four column names from the pandas DataFrame listed in alphabetical order.

#get column names in reverse alphabetical order
sorted(df, reverse=True)

['team', 'points', 'playoffs', 'assists']

Example 3: Get Column Names with Specific Data Type

You can use the following syntax to view the data type of each column in the DataFrame:

#view data type of each column
df.dtypes

team        object
points       int64
assists      int64
playoffs      bool
dtype: object

You can then use the select_dtypes() function to only get the column names with a specific data type.

For example, we can use the following syntax to only get the column names that have a data type of int64 or bool:

#get all columns that have data type of int64 or bool
list(df.select_dtypes(include=['int64', 'bool']))

['points', 'assists', 'playoffs']

The result is a list of column names that have a data type of int64 or bool.

x