Open In App
Related Articles

Python – Read CSV Columns Into List

Improve Article
Save Article
Like Article

CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. In this article, we will read data from a CSV file into a list. We will use the panda’s library to read the data into a list. 

File Used: file.

Method 1: Using Pandas

Here, we have the read_csv() function which helps to read the CSV file by simply creating its object. The column name can be written inside this object to access a particular column, the same as we do in accessing the elements of the array. Pandas library has a function named as tolist() that converts the data into a list that can be used as per our requirement. So, we will use this to convert the column data into a list. Finally, we will print the list.


  • Import the module.
  • Read data from CSV file.
  • Convert it into the list.
  • Print the list.

Below is the implementation:


# importing module
from pandas import *
# reading CSV file
data = read_csv("company_sales_data.csv")
# converting column data to list
month = data['month_number'].tolist()
fc = data['facecream'].tolist()
fw = data['facewash'].tolist()
tp = data['toothpaste'].tolist()
sh = data['shampoo'].tolist()
# printing list data
print('Facecream:', fc)
print('Facewash:', fw)
print('Toothpaste:', tp)
print('Shampoo:', sh)


Method 2: Using csv module

In this method we will import the csv library and open the file in reading mode, then we will use the DictReader() function to read the data of the CSV file. This function is like a regular reader, but it maps the information to a dictionary whose keys are given by the column names and all the values as keys. We will create empty lists so that we can store the values in it. Finally, we access the key values and append them into the empty lists and print that list.


# importing the module
import csv
# open the file in read mode
filename = open('company_sales_data.csv', 'r')
# creating dictreader object
file = csv.DictReader(filename)
# creating empty lists
month = []
totalprofit = []
totalunit = []
# iterating over each row and append
# values to empty list
for col in file:
# printing lists
print('Month:', month)
print('Moisturizer:', totalprofit)
print('Total Units:', totalunit)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 Sep, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials