26. How to Read APIs, JSON and Zipped CSVs into pandas
26.7 Practice, Tip and Close
Why. File, zip, and JSON paths should feel interchangeable before project week.
What. One lab: write JSON + zip, load both to DataFrames, compare shapes, export a tiny CSV.
How.
Chapter practice lab
- Recreate
data/raw/status_sample.jsonwith ≥8 fictional orders across six Maharashtra cities (mix Open/Delivered). - Load with
json_normalizeintodf_api. - Build
orders_mini.csv.zipand load intodf_zip. - Harmonize columns; concat or compare city counts.
- Export
outputs/api_zip_city_counts.csv. - Write three bullets: timeout habit, which JSON key held rows, how you opened the zip.
import json
import zipfile
from pathlib import Path
import pandas as pd
Path("data/raw").mkdir(parents=True, exist_ok=True)
Path("outputs").mkdir(exist_ok=True)
payload = {
"orders": [
{"order_id": "BLK-61", "city": "Pune", "status": "Delivered", "amount_inr": 189},
{"order_id": "BLK-62", "city": "Nashik", "status": "Open", "amount_inr": 95},
{"order_id": "AMN-63", "city": "Kolhapur", "status": "Delivered", "amount_inr": 210},
{"order_id": "BLK-64", "city": "Solapur", "status": "Delivered", "amount_inr": 70},
{"order_id": "BLK-65", "city": "Nagpur", "status": "Open", "amount_inr": 150},
{"order_id": "BLK-66", "city": "Sambhaji Nagar", "status": "Delivered", "amount_inr": 120},
{"order_id": "BLK-67", "city": "Pune", "status": "Delivered", "amount_inr": 99},
{"order_id": "AMN-68", "city": "Nashik", "status": "Delivered", "amount_inr": 130},
]
}
Path("data/raw/status_sample.json").write_text(json.dumps(payload), encoding="utf-8")
df_api = pd.json_normalize(payload["orders"])
csv_path = Path("data/raw/orders_mini.csv")
df_api.to_csv(csv_path, index=False)
zip_path = Path("data/raw/orders_mini.csv.zip")
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as z:
z.write(csv_path, arcname="orders_mini.csv")
df_zip = pd.read_csv(zip_path)
counts = df_zip.groupby("city", as_index=False).size().rename(columns={"size": "orders"})
counts.to_csv("outputs/api_zip_city_counts.csv", index=False)
print(df_api.shape, df_zip.shape)
print(counts)
Practice task
Zoya: “Nested customer.city in JSON + a zip of flat CSV. City counts must match after normalize.” No real production API key.
Ravindra Bagale's Tip
Think of an API as a live tap, a zip as a packed bottle and pandas as the place where you tell the story. Keep a small folder of tiny JSON and zip samples, so you can practise offline without calling any server. Never put a real API key or any secret in Git, even in a practice project.
Ravindra Bagale's Tip – मराठी
API म्हणजे live नळ, zip म्हणजे पॅक केलेली बाटली आणि pandas म्हणजे जिथे तुम्ही data ची गोष्ट सांगता ती जागा, असं समजा. छोट्या JSON आणि zip samples चा एक छोटा folder ठेवा, म्हणजे कोणत्याही server ला call न करता offline सराव करता येईल. खरी API key किंवा कोणतंही secret Git मध्ये कधीही ठेवू नका, अगदी practice project मध्ये सुद्धा.
Ravindra Bagale's Tip – हिंदी
API को live नल, zip को पैक की हुई बोतल और pandas को वह जगह समझो जहाँ आप data की कहानी बताते हो। छोटे JSON और zip samples का एक छोटा folder रखो, ताकि किसी भी server को call किए बिना offline practice कर सको। असली API key या कोई भी secret Git में कभी मत रखो, practice project में भी नहीं।
Thodkyaat sangaycha tar (quick recap)
- APIs and zips still land in the same DataFrame kitchen
requests: timeout, status, env secrets- Find the list of records; use
json_normalize read_csvoften reads.csv.zipdirectly- Wrong grain =
DataFrame(wrapper_dict) - Save raw files under
data/raw/for replay
Samajla ka? Aata API JSON ani zip CSV donhi DataFrame madhe yenar — pudhe practice raw folder neat theva.