Table of Contents
There are several methods in Pandas that can be used to list all column names in a dataset. These include using the .columns attribute, the .keys() method, the .info() method, and the .head() method. The .columns attribute returns a list of all column names, the .keys() method returns an index object with all column names, the .info() method provides an overview of the dataset including the column names, and the .head() method displays the first few rows of the dataset with column names. These methods can be useful for quickly identifying and accessing column names in a Pandas dataframe.
List All Column Names in Pandas (4 Methods)
You can use one of the following four methods to list all column names of a pandas DataFrame:
Method 1: Use Brackets
[column for column in df]
Method 2: Use tolist()
df.columns.values.tolist()
Method 3: Use list()
list(df)
Method 4: Use list() with column values
list(df.columns.values)
The following examples show how to use each of these methods with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23], 'assists': [5, 7, 7, 9, 12, 9], 'rebounds': [11, 8, 10, 6, 6, 5], 'blocks': [6, 6, 3, 2, 7, 9]}) #view DataFrame df points assists rebounds blocks 0 25 5 11 6 1 12 7 8 6 2 15 7 10 3 3 14 9 6 2 4 19 12 6 7 5 23 9 5 9
Method 1: Use Brackets
The following code shows how to list all column names of a pandas DataFrame using brackets:
[column for column in df] ['points', 'assists', 'rebounds', 'blocks']
Method 2: Use tolist()
The following code shows how to list all column names using the .tolist() function:
df.columns.values.tolist() ['points', 'assists', 'rebounds', 'blocks']
Method 3: Use list()
The following code shows how to list all column names using the list() function:
list(df)
['points', 'assists', 'rebounds', 'blocks']Method 4: Use list() with column values
The following code shows how to list all column names using the list() function with column values:
list(df.columns.values) ['points', 'assists', 'rebounds', 'blocks']
Notice that all four methods return the same results.
Note that for extremely large DataFrames, the df.columns.values.tolist() method tends to perform the fastest.
The following tutorials explain how to perform other common functions with columns of a pandas DataFrame:
Cite this article
stats writer (2024). “What are the different methods to list all column names in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-are-the-different-methods-to-list-all-column-names-in-pandas/
stats writer. "“What are the different methods to list all column names in Pandas?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/what-are-the-different-methods-to-list-all-column-names-in-pandas/.
stats writer. "“What are the different methods to list all column names in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-are-the-different-methods-to-list-all-column-names-in-pandas/.
stats writer (2024) '“What are the different methods to list all column names in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-are-the-different-methods-to-list-all-column-names-in-pandas/.
[1] stats writer, "“What are the different methods to list all column names in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. “What are the different methods to list all column names in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
