A Python project – CSV Data Analysis

Lesson 5 of 15 · Python projects

In this project we will learn how to read and analyse data from CSV files in Python.

CSV files are often used to store data in the form of a table (similar to Excel).

In this project we will use:
  • working with files
  • the csv module
  • loops
  • basic data analysis

An example CSV file

Let us assume we have a file called sales.csv.


product,price
Laptop,900
Phone,500
Tablet,300
Monitor,200

The program code


import csv

with open("sales.csv") as file:

    reader = csv.reader(file)

    next(reader)

    total = 0
    count = 0

    for row in reader:

        price = int(row[1])

        total += price
        count += 1

average = total / count

print("Total value:", total)
print("Average price:", average)

How this code works

  • csv.reader() reads the rows of the CSV file
  • next() skips the header of the table
  • the loop goes through all the rows
  • we calculate the total and the average value
You can extend the program by finding the most expensive product or making a chart of the data.

Possible improvements to the project

  • find the maximum price
  • find the minimum price
  • make a chart
  • load a bigger dataset

Next project

How to make the project your own:
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.