Pandas get column names

By ayed_amira , on 02/16/2020 , updated on 09/10/2020 , 1 comment - 2 minutes to read
pandas python

Pandas get column names : When analyzing large datasets, it may be necessary to obtain column names to perform certain operations on the dataset. In this article, I will show you four ways to retrieve column names in a Pandas dataframe.

Let’s start by creating a relatively simple dataset.

# Import pandas package  
import pandas as pd  
# Import dataset from csv
# Displays the first lines 
df_head = data.head()  
dataset example pandas

Now let’s try to get the names of the columns in the above data set.

1st method: Iterate on the names of the column :
# iterating the columns 
for col in df.columns: 

Result :

Pandas get column names
2nd method : Using the .columns function :
# list(df) or 

Result :

Pandas get column names
3rd method : the column.values function returns an index array.

Result :

Pandas get column names
4th method: Using the sorted() function
# using sorted() method 

Result :

Pandas get column names
5th method: Using a tolist() function

Result :

sorted function

If you need help to install the pandas module, I advise you to have a look at this link : https://amiradata.com/pip-install-specific-version/

I advise you to have a look at the Official documentation which is rather well provided 🙂


I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)


Leave a comment

Your comment will be revised by the site if needed.