get column names in python pandas

How to Get Column Names in Pandas Dataframe

Python pandas is a powerful library that allows you to easily work with data in python. Its critical feature is dataframe which is a tabular organization of data, accessed using rows, columns & indexes. Often, while using pandas, both beginners and advanced coders need to get column names of Pandas Dataframe. In this article, we will learn how to get column names in Pandas dataframe.


How to Get Column Names in Pandas Dataframe

Let us say you have the following dataframe in pandas with column names id, age, name, marks

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

There are several ways to get column names in pandas dataframe.

1. Iterating Through Columns

The simplest way is to iterate over columns one by one and print or store each column’s name. You can run a loop through dataframe.columns object for this purpose.

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

# iterating the columns
for col in data.columns:
	print(col)

Here is the output you will get, one column name on each line.

ID
Age
Name
Marks

2. Using Dataframe.columns

As mentioned above, whenever you create a dataframe, you can always use its .columns property to obtain a list of column names. Here is an example where we directly access this object using list function, without looping through it.

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

Alternatively, you can also use dataframe.columns.values for this purpose.

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

You can also use dataframe.columns.values.tolist() function for this purpose.

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

In all the above cases, you will get a list of column names as output.

['ID','Age','Name','Marks']

3. Using sorted method

All the above methods will return column names in the same order as they are present in the dataframe. But if you want to display them in sorted order, then you can do so using sorted function on dataframe.columns property.

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

Here is the output you can expect.

['Age','ID','Marks','Name']

In the above article, we have learnt how to get column names in Pandas Dataframe. You can use any of the above methods as per your requirement.

Also read:

How to Change NGINX Autoindex
How to Manage User Password Expiry & Aging in Linux
How to Remove Yum Repositories
How to Undo or Redo Yum Install on RHEL/CentOS/Fedora
How to Fix Password Authentication Token Manipulation Error

Leave a Reply

Your email address will not be published.