How to get column names in Pandas dataframe

While analyzing the real datasets which are often very huge in size, we might need to get the column names in order to perform some certain operations.

Let’s discuss how to get column names in Pandas dataframe.

First, let’s create a simple dataframe with nba.csv file.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
    
# calling head() method  
# storing in new variable 
data_top = data.head() 
    
# display 
data_top 

chevron_right



Now let’s try to get the columns name from above dataset.

Method #1: Simply iterating over columns

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
# iterating the columns
for col in data.columns:
    print(col)

chevron_right


Output:

 
Method #2: Using columns with dataframe object

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
# list(data) or
list(data.columns)

chevron_right


Output:

 
Method #3: column.values method returs an array of index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
list(data.columns.values)

chevron_right


Output:

 
Method #4: Using tolist() method with values with given the list of columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
list(data.columns.values.tolist())

chevron_right


Output:

 
Method #5: Using sorted() method

Sorted() method will return the list of columns sorted in alphabetical order.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
# using sorted() method
sorted(data)

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.