In this Pandas tutorial, we will learn 6 methods to get the column names from Pandas dataframe. One of the nice things about Pandas dataframes is that each column will have a name (i.e., the variables in the dataset). Now, we can use these names to access specific columns by name without having to know which column number it is.
To access the names of a Pandas dataframe, we can the method columns(). For example, if our dataframe is called df we just type print(df.columns) to get all the columns of the Pandas dataframe.
After this, we can work with the columns to access certain columns, rename a column, and so on. In the next section, before learning the methods for getting the column names of a dataframe, we will import some data to play with.
Importing Data from a CSV File
First, before learning the 6 methods to obtain the column names in Pandas, we need some example data. In this post, we will use Pandas read_csv to import data from a CSV file (from this URL). Now, the first step is, as usual, when working with Pandas to import Pandas as pd.
import pandas as pd
df = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/carData/UN98.csv',
index_col=0)
df.head()
Code language: Python (python)
It is, of course, also possible to read xlsx files using Pandas read_excel method. Another method to get our data into Python is to convert a dictionary to a Pandas dataframe. After you have found the answer on the question “How do I get column names in Pandas?” you will learn how to get column names in six different ways.
How do I get column names in Pandas?
To get the column names in Pandas dataframe you can type <code>print(df.columns)</code> given that your dataframe is named “df”. There are, of course, at least 5 other options for getting the column names of your dataframe (e.g., <code>sorted(df)</code>).
Pandas How to Get the Column Names from the Dataframe:
Now, we are ready to learn how we can get all the names using six different methods. First, we use the DataFrame.columns method to print all names:
1. Get the Column Names Using the columns() Method
Now, one of the simplest methods to get all the columns from a Pandas dataframe is, of course, using the columns method and printing it. In the code chunk below, we are doing exactly this.
print(df.columns)
Code language: Python (python)
Right, the columns method will get the labels of the dataframe. That is, when we use print we will print column names (i.e., the labels). Here’s the result of the above code:

In the next example, we are going to use the keys() method to print all the names in the dataframe:
2. Using the keys() Method
Second, we can get the exact same result by using the keys() method. That is, we will get the column names by the following code as well.
# Dataframe show all columns
print(df.keys())
Code language: Python (python)
In the next example, we will iterate over the DataFrame.columns to print each name on a separate line.
3. By Iterating of the Columns
In the third method, we will simply iterate over the columns to get the column names. As you may notice, we are again using the columns method.
# Get all names
for col_name in df.columns:
print(col_name)
Code language: Python (python)

In the next example, we will get all the names using the list() method together with the df.columns method.
4. Using list() to Print the Names as a list
In the fourth method, on the other hand, we are going to use the list() method to print the column names as a list.
# Print the columns as list
print(list(df.columns))
Code language: Python (python)

Another option, which we will see in the next example, is the tolist() method.
5. Using tolist() to Print the Names as a List
Now, we can use the values method, as well, to get the columns from Pandas dataframe. If we also use the tolist() method, we will get a list, as well.
# Show all columns as list
print(df.columns.values.tolist())
Code language: Python (python)
6. Using sorted() to Get an Ordered List
Now, in the final, and sixth, method to print the names, we will use sorted() to get the columns from a Pandas dataframe in alphabetic order:
# Dataframe show all columns sorted list
sorted(df)
Code language: Python (python)
As previously mentioned, when using sorted we will get this ordered list of column names:

How to Get Values by Column Name:
Now, that we know the column names of our dataframe we can access one column (or many). Here’s how we get the values from one column:
print(df['tfr'].values)
Code language: Python (python)
If we, on the other hand, want to access more than one column we add a list: df[['tfr', 'region']]
How to Rename a Column
In the final example, what we can do when we know the column names of a Pandas dataframe is to rename a column.
df.rename(columns={'tfr': 'TFR'})
Code language: Python (python)
Note, if we want to save the changed name to our dataframe we can add the inplace=True, to the code above. In the video below, you will learn how to use the inplace parameter, as well as all the other things from this post. In a more recent post, you will learn all you need about renaming columns in Pandas dataframe.
Conclusion: Getting all the Column Names with Pandas
Now, in this post, we have learned how to get the column names from a Pandas dataframe. Specifically, we learned why and when this can be useful, 6 different methods to access the column names, and very briefly what we can do when we know the column names. Finally, here’s the Jupyter Notebook with all the example code.
you can also print/get one specific column name using:
> df.columns[#]
where # is the column number
Hey Anibel! Thanks for this comment. Always nice when readers add to the posts with other methods. Like in this case, how to print a specific column.
Best,
Erik
Very insightful.
Thank you, Hicham, for the kind comment. I am glad you liked learning how to get the column names from Pandas dataframe.
Thanks a lot. Just have found sth I was looking for it a lot!
Hey there! Thanks for your comment. I am glad it helped you get the column names from Pandas dataframe.
print(df.dtypes) is a good approach as well. Yes, we will see data types as well.
Hey Denis,
Thanks for your comment and suggestion,
Best,
Erik