L5·Intermediate/Advanced

📊CSV, APIs & Working with Data

csv module, requests basics and a first look at pandas.

csvrequestspandasdata

§ 1csv module

csv.DictReader yields each row as a dict keyed by header; csv.DictWriter writes dicts back. Always open with newline=''.

§ 2HTTP APIs

requests.get(url, timeout=10).json() fetches JSON. Always set a timeout and call raise_for_status().

§ 3pandas

pd.read_csv loads a table into a DataFrame; groupby/agg, filtering and merge cover most data-engineering interview tasks.

1import pandas as pd​
2df = pd.read_csv("sales.csv")​
3print(df.groupby("region")["amount"].sum())​

Worked example

1import csv, io​
2​
3raw = "name,dept,salary\nAda,eng,120\nBob,ops,80\nCy,eng,100\n"​
4totals = {}​
5for row in csv.DictReader(io.StringIO(raw)):​
6 totals[row["dept"]] = totals.get(row["dept"], 0) + int(row["salary"])​
7print(totals)​
8​
9out = io.StringIO()​
10w = csv.DictWriter(out, fieldnames=["dept", "total"])​
11w.writeheader()​
12for d, s in totals.items():​
13 w.writerow({"dept": d, "total": s})​
14print(out.getvalue())​

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 2
csv.DictReader rows are:

Lesson test

Take an 8-question test on “CSV, APIs & Working with Data” — concepts, output prediction, bug spotting and interview questions.