Ravindra BagaleCourses & study guides मराठी Track your progress

26. How to Read APIs, JSON and Zipped CSVs into pandas

Data Science

26.5 Zipped CSVs into pandas

Why. Email caps and GitHub dislike huge CSVs. Ops sends orders_oct.csv.zip. You still want the same cleaning pipeline.

What.

  • pd.read_csv("file.csv.zip") often works (pandas detects zip compression).
  • Or compression="zip" explicitly.
  • For a zip with many members, open with zipfile and pick the CSV name.

How.

Read a classroom zip

  1. Save the zip under data/raw/.
  2. Try pd.read_csv(path) on the .csv.zip.
  3. If multiple files inside, ZipFile.namelist() and choose one.
  4. Same dtype / dayfirst habits as Chapter 10.
  5. Write a clean extract to data/clean/ uncompressed for daily work if you prefer.

Worked example A — Build + read a zip (copy-paste).

import zipfile
from pathlib import Path
import pandas as pd

Path("data/raw").mkdir(parents=True, exist_ok=True)
csv_path = Path("data/raw/orders_mini.csv")
csv_path.write_text(
    "order_id,city,amount_inr\nBLK-50,Pune,189\nBLK-51,Nashik,95\n",
    encoding="utf-8",
)
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 = pd.read_csv(zip_path)
print(df)

Worked example B — Explicit compression.

df2 = pd.read_csv(zip_path, compression="zip")
print(df2.equals(df))

Worked example C — Multiple members.

with zipfile.ZipFile(zip_path) as z:
    print(z.namelist())

Ravindra Bagale's Tip

A file named .csv.zip is a CSV inside a zip, and pd.read_csv() can usually read it directly. If the zip holds more than one file, list them with namelist() and pick the CSV yourself, because zips often also carry a README or junk folders. Keep the same dtype and date habits that you use for a normal CSV.