Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 14/ 160
01. Python & Data Wrangling

Working with CSV, JSON, Parquet & Excel

Read/write common file formats and choose Parquet for efficient columnar analytics storage.

BeginnerCSV/JSON · Parquet3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

CSV and Excel are easy to open but slow and forgetful; Parquet is a faster, smaller format that remembers column types.

Think of it like…

Shipping goods: CSV is loose items in a box you must unpack and sort every time; Parquet is shrink-wrapped, labelled crates where you can pick just the columns you want without opening the rest.

Where you’ve seen it

A daily 2.1 GB events.csv takes 48 seconds to load and 4.5 GB of memory. After a one-time conversion with explicit dtypes to events.parquet (about 310 MB on disk), pd.read_parquet("events.parquet", columns=["user_id", "event_ts", "amount"]) loads only three columns in under 2 seconds, with timestamps already as datetimes.

Words you’ll hear

Columnar storage
Values stored column by column, so you read only what you need and compress well.
dtype / usecols / chunksize
read_csv options to set types, load some columns, or stream in pieces.
json_normalize
Flattens nested JSON records into columns.
Schema
Column names and types stored with a file (Parquet) rather than guessed.

Visual diagram

Loading interactive diagram

Theory & deep dive

Reading CSV, JSON and Excel reliably

pd.read_csv options solve most real files: sep=";" for other delimiters, encoding="utf-8-sig" or "latin-1" for encoding errors (a BOM or Windows files), na_values=[...] for placeholders, dtype={...} to keep IDs as strings, parse_dates=[...] for dates, usecols=[...] to read only some columns, nrows= to peek at big files, and on_bad_lines="skip" or "warn" for malformed rows. For files larger than memory, chunksize=500_000 yields DataFrames you can filter or aggregate piece by piece, or use Polars or DuckDB for out-of-core queries.

pd.read_json handles records and lines (lines=True for newline-delimited JSON), while nested structures need pd.json_normalize(data, record_path="items", meta=["order_id"]) to explode a list of line items into rows keeping parent fields. pd.read_excel(path, sheet_name="Sales", header=2, usecols="A:F") selects sheets and ranges; Excel files often have title rows, merged cells, totals rows and numbers stored as text, so inspect before trusting, and avoid Excel as a data store for anything automated.

A defensive CSV read

df = pd.read_csv("events.csv", dtype={"user_id": "string"}, parse_dates=["event_ts"], na_values=["N/A", ""], usecols=["user_id", "event_ts", "amount"], encoding="utf-8-sig") fixes types and loads only needed columns.

Key takeaway: Declare dtypes, dates, NA values and columns when reading; stream big files in chunks.

Why and how to use Parquet

Parquet stores each column contiguously with its type and applies dictionary and run-length encoding plus compression (Snappy by default, Zstd for smaller files). Benefits: typically 5 to 10 times smaller than CSV, 10 to 50 times faster to read, columns can be selected (columns=[...]), filters can skip row groups (filters=[("event_date", ">=", "2026-11-01")]), and dtypes (including datetimes and categories) round-trip exactly. It works across pandas, Polars, DuckDB, Spark and warehouses.

Write with df.to_parquet("events.parquet", compression="zstd", index=False). For large, growing data, write a partitioned dataset (partition_cols=["event_date"]), so queries for one day read one folder. Parquet isn't human-readable and isn't designed for in-place updates; keep CSV or JSON for interchange and inspection, and use Parquet as the working format. Mind schema evolution: adding columns is fine, but changing a type across files requires care.

One-time conversion, many fast reads

df.to_parquet("events.parquet", index=False) once, then pd.read_parquet("events.parquet", columns=["user_id", "amount"]) in every notebook, instead of re-parsing a 2 GB CSV each time.

Key takeaway: Convert once to typed, compressed Parquet; read only the columns and rows you need.

How it works, step by step

Step 1 of 4
Inspect

1. Peek before loading

Read a few rows or nrows to check delimiter, encoding, header and types.

pd.read_csv(path, nrows=1000)
Key numbers at this step
Look for
encoding and bad lines

When to use it & trade-offs

What it is

Pandas reads and writes the formats data arrives in: CSV (read_csv), JSON (read_json, json_normalize for nested records), Excel (read_excel, needs openpyxl) and Parquet (read_parquet, needs pyarrow). CSV and JSON are text, row-oriented and untyped; Parquet is binary, columnar, compressed and typed, so it's smaller, much faster to read and keeps dtypes. Good practice is to accept messy inputs at the edge, convert once, and store the cleaned data as Parquet.

Why it matters: Reading a 2 GB CSV repeatedly in every notebook wastes minutes and loses types (dates become strings, IDs become floats). Choosing formats well speeds up analysis by an order of magnitude, shrinks storage and avoids repeating parsing bugs. Encoding errors, delimiters, bad lines and Excel quirks are everyday problems with simple fixes.

When to reach for it

  • Parquet for stored, repeatedly read datasets
  • CSV or JSON for exchange with other systems
  • Chunked reads or DuckDB/Polars for files larger than memory

When to avoid or be careful

  • Re-parsing huge CSVs in every notebook
  • Letting pandas guess types for IDs and dates
  • Using Excel as an automated data source

File formats compared

CSV
Best for: Interchange and quick inspection

Strengths: Universal, human-readable.

Watch out: Slow, large, no types; encoding and delimiter issues.

JSON / JSON Lines
Best for: APIs and nested or semi-structured records

Strengths: Flexible structure.

Watch out: Verbose; needs flattening for analysis.

Parquet
Best for: Analytics and ML working datasets

Strengths: Small, fast, typed, columnar, filterable.

Watch out: Binary; not for manual editing.

Excel
Best for: Business hand-offs and small reports

Strengths: Familiar to stakeholders.

Watch out: Row limits, hidden formatting, error-prone as a data source.

Interview signals & numbers to know

When this topic comes up

  • “Loading the CSV takes minutes”
  • “UnicodeDecodeError when reading a file”
  • “Flatten this nested JSON”
  • Parquet vs CSV
  • Out-of-memory reads

Benchmarks & rules of thumb

≈ 5–10× -
typical Parquet size reduction vs CSV
48 s → 2 s -
load time in the example
500,000 -
a common chunksize

Common mistakes & follow-ups

Common interview mistakes

  • Reading ID columns without dtype, so leading zeros vanish and big integers become floats.
  • Ignoring UnicodeDecodeError by switching to errors="ignore" and silently dropping characters.
  • Writing to Parquet with the index included by accident and later needing to drop it.

Likely interviewer follow-ups

  • Why is Parquet faster than CSV?
  • How would you load a file bigger than memory?
  • How do you turn a list of nested orders into a flat table?
Interview tip: Quote one before-and-after timing and file size from a real conversion. It makes "use Parquet" a measured recommendation.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Working with CSV, JSON, Parquet & Excel with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Date & Time Manipulation
Next topic
Web Scraping & API Data Extraction