The Wayback Machine - https://web.archive.org/web/20240204183220/https://www.geeksforgeeks.org/how-to-get-column-names-in-pandas-dataframe/
Open In App
Related Articles

How to get column names in Pandas dataframe

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 

Python3




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


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

Simply iterating over columns 

Python3




# 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)


Output:

   

 Method #2: Using columns attribute with dataframe object 

Python3




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


Output:

    

Method #3: Using keys() function: It will also give the columns of the dataframe.

Python3




# Import pandas package
import pandas as pd
   
# making data frame
   
# calling keys() function
print(data.keys())


Output:

Method #4:  column.values method returns an array of index. 

Python3




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


Output: 

 

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

Python3




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


Output:

   

 Method #6: Using sorted() method : sorted() method will return the list of columns sorted in alphabetical order. 

Python3




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


Output:

 


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 29 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials