Data: analysing a CSV file
Most data arrives as CSV: one line per record, columns separated by commas. csv.DictReader reads each line as a dictionary whose keys are the column names. Here, io.StringIO makes a piece of text act like a file.
The most common operation in analysis is grouping: adding up a value per category. A dictionary of totals and dict.get(key, 0) are enough. Careful: everything the CSV reads is text, so convert it with int() or float(). This is what groupby does in pandas.
import csv, io
data = io.StringIO("""city,sales
Paris,120
Lyon,80
Paris,60
Lille,40
""")
totals = {}
for row in csv.DictReader(data):
totals[row["city"]] = totals.get(row["city"], 0) + int(row["sales"])
for city, total in sorted(totals.items(), key=lambda kv: kv[1], reverse=True):
print(city, total)