CSV stands for Comma Separated Values and CSV files are essentially text files which are used to store data in a tabular fashion using commas (,) as delimiters. CSV is a file format and all the files of this format are stored with a .csv extension. It is a very popular and extensively used format for storing the data in a structured form. CSV files find a lot of applications in Machine Learning and Statistical Models. Python has a library dedicated to deal with operations catering to CSV files such as reading, writing, or modifying them. Following is an example of how a CSV file looks like.

This article deals with the different ways to get column names from CSV files using Python. The following approaches can be used to accomplish the same :
- 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
Method 1:
Using this approach, we first read the CSV file using the CSV library of Python and then output the first row which represents the column names.
Python3
import csv
with open ( 'data.csv' ) as csv_file:
csv_reader = csv.reader(csv_file, delimiter = ',' )
list_of_column_names = []
for row in csv_reader:
list_of_column_names.append(row)
break
print ( "List of column names : " ,
list_of_column_names[ 0 ])
|
Output:
List of column names : ['Column1', 'Column2', 'Column3']
Method 2:
Under the second approach, we use the DictReader function of the CSV library to read the CSV file as a dictionary. We can simply use keys() method to get the column names.
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.
Python3
import csv
with open ( 'data.csv' ) as csv_file:
csv_reader = csv.DictReader(csv_file)
dict_from_csv = dict ( list (csv_reader)[ 0 ])
list_of_column_names = list (dict_from_csv.keys())
print ( "List of column names : " ,
list_of_column_names)
|
Output :
List of column names : ['Column1', 'Column2', 'Column3']
Method 3:
Under this approach, we read the CSV file as a data frame using the pandas library of Python. Then, we just call the column’s method of the data frame.
Python3
import pandas as pd
df = pd.read_csv( 'data.csv' )
list_of_column_names = list (df.columns)
print ( 'List of column names : ' ,
list_of_column_names)
|
Output :
List of column names : ['Column1', 'Column2', 'Column3']
The Data Frame looks as follows :

The CSV file as a Data Frame