Python Pandas – Get column headers from a Pandas data frame

In this post we will be creating a Python script that will retrieve the column headings from a Pandas data frame, Pandas is a Python library which is an amazing tool for data analysis and manipulation.

In this example our data will be a CSV file that we will be reading from in Python, a sample of the data can be seen in the image below. The column headings that we’ll be retrieving have been marked in bold text.

Sample data

To load the CSV data in to a Pandas data frame we would use the snippet of code below.

import pandas as pd

df = pd.read_csv('Sample.csv')

The snippet of code below will retrieve all column headings and return a list as output.

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

The code above would return the following as output.

['Rank', 'Name', 'Platform', 'Year', 'Genre', 'Publisher', 'NA_Sales', 'EU_Sales', 'JP_Sales', 'Other_Sales', 'Global_Sales']
>>> 

Leave a Reply