How to Get Column Names in Pandas DataFrame (6 Ways)

In a Pandas DataFrame, every column has a label or name that indicates the kind of value it contains or represents. When we need to programmatically change the values of all columns or access all columns by name, getting the column names will be helpful. Also, we might need to retrieve the column names when examining real datasets, which are frequently very large in size, in order to carry out specific actions.

In this article, we will look at six ways to get column names in Pandas DataFrame so that you can use the one that best suits you. Let’s get started.

Also Read: Create a Pandas DataFrame from Lists

Getting Column Names in Pandas DataFrame

For Getting Column Names in Pandas DataFrame let’s first create a DataFrame using a dictionary.

import pandas as pd
data = {
    'Name':['Akshay', 'Raj', 'Rajat'],
    'Marks':[45,78,90],
    'City':['Pune','Delhi','Mumbai']
}
df = pd.DataFrame(data)
df

Here we created a dictionary data containing three key-value pairs. For converting the data dictionary into a DataFrame, we used pd and then we put the dictionary into pd.DataFrame() for conversion and stored it in the df variable. In last, we displayed the DataFrame.

Output:

Sample DataFrame

1. Getting Column Names using For Loop

We can get the column names in Pandas DataFrame by a very straightforward approach which is iterating over the columns using the for loop.

Example:

for col in df.columns:
    print(col)

Here we have used the df.columns method for iteration. df.columns method returned the column names as a pandas ‘Index’ object and we iterated through it to get printed the names of the columns.

Output:

Getting Column Names using For Loop

2. Getting Column Names using keys() Method

We can also use the inbuilt method which is the keys() method to get the column names. It is one of the shortest methods to print the column names from our DataFrames. The keys() method is generally used with the dictionary.

Example:

print(df.keys())

Output:

Getting Column Names using keys() Method

We can see that we have got our column names in the form of an ‘Index’ object.

3. Getting Column Names using Columns Attribute

As the heading suggests, we will use the columns attribute with the DataFrame object df to get the column names.

Example:

print(df.columns)

Output:

Getting Column Names using Columns Attribute

Like the last example, this time also we got the column names in the form of an ‘Index’ object.

4. Getting Column Names using list() Method

In the above examples, we will get the column names in the form of an ‘Index’ object but now we will be using the list() method to convert it in the form of a list.

Example:

print(list(df.columns))

Here we use df.columns which will return us the column names in the form of an ‘Index’ object then we pass df.columns into the list() method to typecast the ‘Index’ object to a list.

Output:

Getting Column Names using list() Method

5. Getting Column Names using tolist() Method

Alternatively to the list() method, we can also get the column names in the form of a list using the tolist() method.

Example:

print(df.columns.values.tolist())

Here df.columns.values returned the column names in the form of a NumPy array then we used tolist() method at the end which returned the column names in the form of a list i.e. we typecasted the result of df.columns.values to a list.

Output:

Getting Column Names using tolist() Method

6. Getting Column Names using sorted() Method

Using sorted() method we can get the column names from the DataFrame in the form of a list but this time column names will be in ascending order.

Example:

import pandas as pd
print(sorted(df))

Here we have just written the sorted() method and inside it, we have provided the DataFrame object df from which we want to extract the column names, and when we ran the code we got the list of our column names in alphabetical order.

Output:

Getting Column Names using sorted() Method

Summary

The most frequently used term when working with databases or DataFrames is a column or attribute name. The fundamental reason is that they provide the category of data existing under the column with a valid meaning. Column names are therefore beneficial in interpreting the data. We have discussed six ways of getting column names from Pandas DataFrame with examples. After reading this tutorial, we hope you can easily get the column names of a Pandas DataFrame in Python.

Reference

https://stackoverflow.com/questions/19482970/get-a-list-from-pandas-dataframe-column-headers

Priyanshu Singh
Priyanshu Singh
Articles: 41