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

How to Get Column Names in Pandas

Last Updated : 28 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform some certain operations. Let’s discuss how to get column names in the Pandas .

Syntax to Get Column Names in Pandas

# Get column names as an Index object
column_names = df.columns

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

Python
# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") 
    
# calling head() method  
# storing in new variable 
data_top = data.head() 
    
# display 
data_top 

Output

Get Column Names in Pandas Now let’s try to get the columns name from above dataset.

Retrieving column names while working with data in python can be done for various purpose such as for analyzing the data or to manipulate the data so if you wish to learn more such manipulating techniques you can checkout our comprehensive course Complete Machine Learning & Data Science Program

Get a list from Pandas DataFrame column

Example 1: Get Column Names from CSV

We can use Pandas Dataframe columns Property to get all the columns from a CSV fie in the form of list.

Python
# Import pandas package 
import pandas as pd 

# making data frame 
data = pd.read_csv("nba.csv") 

# list(data) or
list(data.columns)

Output:

Example 2: Iterate Over Columns of Pandas Dataframe

Here, we are loading the CSV file and iterating each column of CSV using the df.columns .

Python
# 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:

Name 
Team
Number
Position
Age
Height
Weight
College
Salary

Get Column Names in Pandas using keys()

It will also give the columns of the dataframe.

Python
# Import pandas package 
import pandas as pd 

# making data frame 
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") 

# calling keys() function
print(data.keys())

Output:

Index([Name, Team, Number, Position, Age, Height, Weight, College, Salary], dtype='object')

Get Column Names in Pandas column.values

The column.values method returns an array of index.

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

Output

[Name, Team, Number, Position, Age, Height, Weight, College, Salary]

Pandas List Column Names using tolist() Method

Using tolist() method with values with given the list of columns.

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

Output

 [Name, Team, Number, Position, Age, Height, Weight, College, Salary]

Pandas Print Column Names using sorted() Method

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

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

Output

[Name, Team, Number, Position, Age, Height, Weight, College, Salary]


Previous Article
Next Article

Similar Reads

Get column index from column name of a given Pandas DataFrame
In this article we will see how to get column index from column name of a Dataframe. We will use Dataframe.columns attribute and Index.get_loc method of pandas module together. Syntax: DataFrame.columns Return: column names index Syntax: Index.get_loc(key, method=None, tolerance=None) Return: loc : int if unique index, slice if monotonic index, els
2 min read
Change column names and row indexes in Pandas DataFrame
Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measurement of some instance while column is a vector which
4 min read
How to Sort a Pandas DataFrame based on column names or row index?
Pandas dataframe.sort_index() method sorts objects by labels along the given axis. Basically, the sorting algorithm is applied to the axis labels rather than the actual data in the Dataframe and based on that the data is rearranged. Creating Pandas Dataframe Create a DataFrame object from the Python list of tuples with columns and indices, say colu
3 min read
Remove spaces from column names in Pandas
Removing spaces from column names in pandas is not very hard we easily remove spaces from column names in pandas using replace() function. We can also replace space with another character. Let's see the example of both one by one. Example 1: remove the space from column name Python Code # import pandas import pandas as pd # create data frame Data =
1 min read
Pandas - Remove special characters from column names
Let us see how to remove special characters like #, @, &, etc. from column names in the pandas data frame. Here we will use replace function for removing special character. Example 1: remove a special character from column names Python Code # import pandas import pandas as pd # create data frame Data = {'Name#': ['Mukul', 'Rohan', 'Mayank', 'Sh
1 min read
Add column names to dataframe in Pandas
In this article, we are going to see how to add column names to a dataframe. Let us how to add names to DataFrame columns in Pandas. Add Column Names to Pandas DataframeBelow are the steps and methods by which we can add column names in the Pandas dataframe in Python: Creating the DataFrameLet's first create an example DataFrame for demonstration r
2 min read
How to get column and row names in DataFrame?
While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations. Note: For downloading the nba dataset used in the below examples Click Here Getting row names in Pandas dataframe First, let's create a simple dataframe with nba.csv C/C++ Code #
3 min read
Get column names from CSV using Python
CSV stands for Comma Separated Values and CSV files are essentially text files which are used to store data in a tabular fashion using commas (,) as delimiters. CSV is a file format and all the files of this format are stored with a .csv extension. It is a very popular and extensively used format for storing the data in a structured form. CSV files
3 min read
Get column names from PostgreSQL table using Psycopg2
This article is an illustration of how to extract column names from PostgreSQL table using psycopg2 and Python. Used table for demonstration: Example 1: First, we connect the PostgreSQL database using psycopg2.connect() method, then we create a cursor using cursor() method, after that we use the cursor() then extract the first element from it using
1 min read
How to get column names from SQLAlchemy?
In this article, we will discuss how to get column names using SQLAlchemy in Python. SQLAlchemy is an open-source SQL toolkit and object-relational mapper for the Python programming language released under the MIT License. It gives full power and flexibility of SQL to an application. To follow along with this article, we need to have sqlalchemy and
3 min read
three90RightbarBannerImg