Introduction

In this article, we will learn how to get the column name from a given dataset in pandas. We have previously learned that Pandas is a Python library used for working with data sets. It offers tools for data exploration, cleaning, analysis, and manipulation.

How to get or print Pandas DataFrame Column Names?

DataFrame.columns.values method can be used to obtain the Pandas DataFrame Column Names, and to-list can be used to obtain the names as a list (). Each column in a Pandas DataFrame has a label or name that indicates the kind of value the column contains or represents. Obtaining the column names is helpful if you wish to change the values of all columns or have programmatic access to all columns by name. I'll describe various approaches to extracting column names from Pandas DataFrame headers in this article along with examples.

Use DataFrame.columns.values.tolist() to retrieve a list of columns from the DataFrame header. The statement's many parts are individually explained below.

  • .columns returns an Index object with column names. This preserves the order of column names.
  • .columns.values returns an array and this has a helper function .tolist() that returns a list of column names.

The examples that follow show you how to quickly retrieve column names from a pandas DataFrame. To output it to the terminal, simply use the print() statement.

# Below are some quick examples

# Get the list of all column names from headers
column_names = list(df.columns.values)

# Get the list of all column names from headers
column_names = df.columns.values.tolist()

#Using list(df) to get the column headers as a list
column_names = list(df.columns)

#Using list(df) to get the list of all Column Names
column_names = list(df)

# Dataframe show all columns sorted list
column_names=sorted(df)

# Get all Column Header Labels as List
for column_headers in df.columns:
    print(column_headers)
   
column_names = df.keys().values.tolist()

# Get all numeric columns
numeric_columns = df._get_numeric_data().columns.values.tolist()

# Simple Pandas Numeric Columns Code
numeric_columns=df.dtypes[df.dtypes == "int64"].index.values.tolist()

Create a Pandas DataFrame from Dict with a few rows and with columns names Courses, Fee, Duration, and Discount.

import pandas as pd
import numpy as np

technologies= {
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
    'Fee' :[22000,25000,23000,24000,26000],
    'Duration':['30days','50days','30days', None,np.nan],
    'Discount':[1000,2300,1000,1200,2500]
          }
df = pd.DataFrame(technologies)
print(df)

pandas Get Column Names

With the help of the Python list() function, you can obtain the column names from a Pandas DataFrame. Once you have the data, you can print it using the print() statement. This statement's df.columns attribute produces an Index object, a fundamental object that contains axis labels, so let me briefly explain what is happening. In our example, the property Index.values of the Index object gives column names in an array of data.

Keep in mind that df.columns maintains the columns' original arrangement. Either one can be used to turn an array of column names into a list.

Use list or toList() on an array object (array object).

# Get the list of all column names from headers
column_headers = list(df.columns.values)
print("The Column Header :", column_headers)

Yields below output.

The Column Header : ['Courses', 'Fee', 'Duration', 'Discount']

You can also use df.columns.values.tolist() to get the DataFrame column names.

# Get the list of all column names from headers
column_headers = df.columns.values.tolist()
print("The Column Header :", column_headers)

Get Column Names in Sorting order

Use the sorted(df) function to obtain a list of column names in sorted order. column names are returned by this function in alphabetical order.

# Dataframe show all columns sorted list
col_headers=sorted(df)
print(col_headers)

Yields below output. Notice the difference of output from above.

['Courses', 'Discount', 'Duration', 'Fee']

Access All Column Names by Iterating

You can perform the iteration over all columns and application of a function as shown below.

# Get all Column Header Labels as List
for column_headers in df.columns:
    print(column_headers)

Yields below output.

Courses
Fee
Duration
Discount


Example of pandas Get Columns Names

import pandas as pd
import numpy as np

technologies= {
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
    'Fee' :[22000,25000,23000,24000,26000],
    'Duration':['30days','50days','30days', None,np.nan],
    'Discount':[1000,2300,1000,1200,2500]
          }
df = pd.DataFrame(technologies)
print(df)

# Get the list of all column names from headers
column_headers = list(df.columns.values)
print("The Column Header :", column_headers)

# Get the list of all column names from headers
column_headers = df.columns.values.tolist()
print("The Column Header :", column_headers)

#Using list(df) to get the column headers as a list
column_headers = list(df.columns)

#Using list(df) to get the list of all Column Names
column_headers = list(df)

# Dataframe show all columns sorted list
col_headers=sorted(df)
print(col_headers)

# Get all Column Header Labels as List
for column_headers in df.columns:
    print(column_headers)
   
column_headers = df.keys().values.tolist()
print("The Column Header :", column_headers)

# Get all numeric columns
numeric_columns = df._get_numeric_data().columns.values.tolist()
print(numeric_columns)

# Simple Pandas Numeric Columns Code
numeric_columns=df.dtypes[df.dtypes == "int64"].index.values.tolist()
print(numeric_columns)


write your code here: Coding Playground

Conclusion

In this article, you learned how to get or print the column names using the functions df.columns, list(df), and df.keys. You also learned how to get the names of all the integer columns and how to get the column names sorted, among other things.