Skip to content

Intermediate · Working with data · topic 31 of 50 · 25 min

CSV files and simple statistics

Explanation

CSV (comma-separated values) is the simple table format that Excel and Google Sheets can save and open. The csv module reads it row by row and correctly handles commas inside quoted values. The statistics module then gives you the mean, median, mode and standard deviation in one line each.

What a CSV file looks like

output
name,subject,marks
Asha,Maths,82
Ravi,Maths,67
"Sharma, Neha",Maths,91
One row per line, columns separated by commas, the first line is the header.
Think of it like this: A CSV file is like the register in a kirana shop. Each line is one entry, and instead of ruled columns, a comma separates the date, the item and the amount. Any program, from Excel to Python, can read such a register.

Reading rows with csv.reader

python
import csv, io
text = "name,marks\nAsha,82\nRavi,67\nMeena,91\n"
reader = csv.reader(io.StringIO(text))
header = next(reader)          # the first row
print(header)
for row in reader:
    print(row[0], int(row[1]))
Each row is a list of strings. next(reader) takes out the header row.

csv.DictReader: rows as dictionaries

python
import csv, io
text = "name,marks\nAsha,82\nRavi,67\nMeena,91\n"
total = 0
for row in csv.DictReader(io.StringIO(text)):
    print(row)
    total += int(row["marks"])
print("Total:", total)
DictReader uses the header as keys, so you write row["marks"] instead of row[1].
Careful: Every value from a CSV is a string. "82" + "67" gives "8267", not 149. Convert numbers with int() or float() before doing maths.

Why not just split(",")?

python
import csv
line = '"Sharma, Neha",Maths,91'
print(line.split(","))
print(next(csv.reader([line])))
split breaks the quoted name into two pieces; csv knows the comma is part of the name.
Tip: csv.reader accepts any list of lines, so csv.reader(text.splitlines()) works as well as io.StringIO(text). On a real file, open it with newline="" as the csv docs recommend: open("marks.csv", newline="", encoding="utf-8").

The statistics module

FunctionIn plain wordsFor 60, 70, 70, 90, 100
meanthe average: add all and divide by how many78
medianthe middle value after sorting70
modethe value that appears most often70
stdevhow spread out the values areabout 16.4
python
import statistics as st
marks = [60, 70, 70, 90, 100]
print(st.mean(marks))
print(st.median(marks))
print(st.mode(marks))
print(round(st.stdev(marks), 1))
mean, median and mode answer "what is typical"; stdev answers "how different".

The standard deviation (stdev) is a number for spread. A small stdev means most values sit close to the average, like a class where everyone scored 70 to 80. A large stdev means the values are far apart, like a class with some 30s and some 95s.

Tip: The median is often fairer than the mean when one value is huge. If nine friends spend about 200 rupees each and one spends 5,000, the mean is pulled up a lot, but the median stays near 200.

Putting it together: monthly expenses

python
import csv, io, statistics
text = """month,category,amount
Jan,food,4200
Jan,travel,1500
Feb,food,3900
Feb,travel,2100
Mar,food,4500
Mar,travel,1200
"""
food = []
for row in csv.DictReader(io.StringIO(text)):
    if row["category"] == "food":
        food.append(int(row["amount"]))
print("Food per month:", food)
print("Average:", statistics.mean(food))
print("Median:", statistics.median(food))
Read, filter, convert, then summarise.

Watch the lesson

CSV files and simple statistics · 1/6

CSV and statistics

Most real data comes as tables. In this lesson you will read CSV tables and summarise them with statistics.

Prefer a video by a teacher? Find free videos on “python csv module dictreader statistics mean median” on YouTube ↗

In real life: A class marks report

Teacher Anjali exports her class marks from Google Sheets as CSV. In a few lines Python reads the file, works out the class average and median, and lists the students who scored above the average, ready for the parents meeting.

python
import csv, io, statistics
data = """name,marks
Asha,82
Ravi,67
Meena,91
Kabir,58
Zoya,74
"""
rows = list(csv.DictReader(io.StringIO(data)))
marks = [int(r["marks"]) for r in rows]
avg = statistics.mean(marks)
print("Average:", round(avg, 1), "| Median:", statistics.median(marks))
print("Above average:", [r["name"] for r in rows if int(r["marks"]) > avg])
Average: 74.4 | Median: 74, and two students above it.

Practice (0/3 done)

Task 1 · Code · Python

Write column_total(csv_text, column) that reads the CSV text (first line is the header) with csv.DictReader and returns the sum of the given column as an int.

Task 2 · Code · Python

Write mark_stats(csv_text) for CSV text with the columns name,marks. Return a tuple (mean, median) of the marks, with the mean rounded to 1 decimal place. Use the statistics module.

Task 3 · Code · Python

Write above_average(csv_text) for CSV text with the columns name,marks. Return the list of names whose marks are strictly above the mean, in the order they appear.

Quick quiz

Question 1

1.What does csv.DictReader give you for each row?

Question 2

2.What type are the values that the csv module reads?

Question 3

3.What is statistics.median([10, 20, 1000])?

Question 4

4.What is statistics.mode([3, 5, 5, 7])?

Question 5

5.A class has a very small standard deviation of marks. What does that tell you?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Working with files