Skip to content

How to Create Dictionaries from CSV Data in Python

Learn how to convert CSV files into Python dictionaries using csv.DictReader for cleaner data access and processing with column names.

The CSV (Comma-Separated Values) format is a standard for storing tabular data. While Python can read CSVs as lists of strings, converting them into dictionaries makes the data significantly easier to work with. Using a dictionary allows you to access data by column name (e.g., row['Price']) rather than by integer index (e.g., row[2]), improving code readability and reducing errors if column orders change.

This guide explains how to use the built-in csv module to parse CSV files directly into Python dictionaries.

Prerequisites: Creating a Sample CSV

To follow along, let's first create a simple CSV file named employees.csv. You can create this manually or run the Python script below to generate it.

import csv

data = [
    ['Name', 'Department', 'Salary'],
    ['Alice', 'Engineering', '90000'],
    ['Bob', 'HR', '60000'],
    ['Charlie', 'Engineering', '95000']
]

with open('employees.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)

print("Sample file 'employees.csv' created.")

Output:

Sample file 'employees.csv' created.

Method 1: Using csv.DictReader (Standard Approach)

The csv module provides a specific class called DictReader. It automatically reads the first row of the CSV file as the header (keys) and maps subsequent rows to these keys.

Reading Data Row by Row

import csv

with open('employees.csv', 'r') as file:
    reader = csv.DictReader(file)
    
    for row in reader:
        print(f"{row['Name']} works in {row['Department']}")

Output:

Alice works in Engineering
Bob works in HR
Charlie works in Engineering

Method 2: Handling CSVs Without Headers

Sometimes, CSV files contain only data without a header row. If you use DictReader directly, it will incorrectly treat the first row of data as the headers. You must specify the fieldnames parameter manually.

First, let's create a headerless file:

data_no_header = [['David', 'Marketing'], ['Eve', 'Sales']]
with open('no_header.csv', 'w', newline='') as f:
    csv.writer(f).writerows(data_no_header)

Specifying Fieldnames

import csv


with open('no_header.csv', 'r') as file:
    cols = ['Name', 'Dept']
    reader = csv.DictReader(file, fieldnames=cols)
    
    for row in reader:
        print(dict(row))

Output:

{'Name': 'David', 'Dept': 'Marketing'}
{'Name': 'Eve', 'Dept': 'Sales'}

Method 3: Filtering and Analyzing Data

Once your data is in a list of dictionaries, you can easily filter, sort, or aggregate it using standard Python list comprehensions.

import csv

with open('employees.csv', 'r') as file:
    reader = csv.DictReader(file)
    employees = list(reader)

engineers = [e for e in employees if e['Department'] == 'Engineering']

total_eng_salary = sum(int(e['Salary']) for e in engineers)

print(f"Engineers found: {len(engineers)}")
print(f"Total Engineering Salary: ${total_eng_salary}")

Output:

Engineers found: 2
Total Engineering Salary: $185000

Conclusion

Converting CSV data to dictionaries is a powerful way to write clean, readable data processing scripts.

  1. Use csv.DictReader when your file has headers; it maps columns automatically.
  2. Use the fieldnames parameter if your file lacks headers.
  3. Convert to a list (list(reader)) if you need to filter or sort the data in memory.
  4. Cast types manually, as all CSV values are read as strings.
Navigation