Get column names from CSV using Python
Last Updated :
30 Jun, 2025
CSV (Comma Separated Values) files store tabular data as plain text, with values separated by commas. They are widely used in data analysis, machine learning and statistical modeling. In Python, you can work with CSV files using built-in libraries like csv or higher-level libraries like pandas.
In this article, we will explore the following three methods to extract column names from a CSV file.
- Using Python's CSV library to read the CSV file line and line and printing the header as the names of the columns.
- Reading the CSV file as a dictionary using DictReader and then printing out the keys of the dictionary.
- Converting the CSV file to a data frame using the Pandas library of Python.
Below is the snapshot of the dataset we are going to use in this article for demonstration, you can download it from here.

Using the csv Library
This method reads the CSV file using the csv.reader and then prints the first row, which contains the column names.
Python
import csv
with open('path_for_data.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
header = next(csv_reader)
print("List of column names:", header)
Output:
List of column names : ['Column1', 'Column2', 'Column3']
Explanation:
- csv.reader reads the file line by line.
- next(csv_reader) retrieves the first row, which is assumed to be the header.
Using csv.DictReader
This method reads the CSV as a dictionary, allowing you to extract the keys (column names) directly.
Steps:
- Open the CSV file using DictReader.
- Convert this file into a list.
- Convert the first row of the list to the dictionary.
- Call the keys() method of the dictionary and convert it into a list.
- Display the list.
Python
import csv
with open('data.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
header = list(next(csv_reader).keys())
print("List of column names:", header)
Output :
List of column names : ['Column1', 'Column2', 'Column3']
Explanation:
- csv.DictReader() reads each row as a dictionary (keys = column names).
- next(csv_reader) retrieves the first row.
- .keys() gives you the column headers.
- list(...) converts the dict_keys object to a list.
Using Pandas
Pandas makes it very simple by reading the CSV into a DataFrame and then accessing the .columns attribute.
Python
import pandas as pd
df = pd.read_csv('path_to_data.csv')
header = list(df.columns)
print("List of column names:", header)
Output :
List of column names : ['Column1', 'Column2', 'Column3']
Explanation:
- pd.read_csv() loads the CSV file into a DataFrame.
- df.columns holds the column names, which we convert into a list.
Related articles:
Similar Reads
Convert Text File to CSV using Python Pandas Converting Text File to CSV using Python Pandas refers to the process of transforming a plain text file (often with data separated by spaces, tabs, or other delimiters) into a structured CSV (Comma Separated Values) file using the Python Pandas library.In this article we will walk you through multip
2 min read
Delete a CSV Column in Python The comma-separated values ââ(CSV) file is a delimited text file that uses commas for individual values. Each line of the file is a data record in CSV. This format used for tabular data, rows, and columns, exactly like a spreadsheet. The CSV file stores data in rows and the values ââin each row are
3 min read
Convert CSV to Excel using Pandas in Python Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel. In this article, we will be dealing with the conversion of .csv file into excel (.xlsx). Pandas provide the ExcelWriter class for writing data frame objects to excel sheets. Syntax
1 min read
Python IMDbPY â Getting name from searched company In this article we will see how we can get the name of movie company from the searched list of companies, we use search_company method to find all the related companies. search_company method returns list and each element of list work as a dictionary i.e. they can be queried by giving the key of the
2 min read
Get the substring of the column in Pandas-Python Now, we'll see how we can get the substring for all the values of a column in a Pandas dataframe. This extraction can be very useful when working with data. For example, we have the first name and last name of different people in a column and we need to extract the first 3 letters of their name to c
2 min read
Python - Read CSV Columns Into List CSV (Comma-Separated Values) files are widely used to store tabular data. Each line in a CSV file corresponds to a data record, and each record consists of one or more fields separated by commas. In this article, youâll learn how to extract specific columns from a CSV file and convert them into Pyth
2 min read