Data Ingestion & Export: CSV, Excel, JSON, Parquet & SQL Database Connections
Overview & Learning Objectives
Data analysts ingest data from heterogeneous operational systems: legacy CSV exports, transactional SQL databases, Excel spreadsheets, REST API JSON payloads, and enterprise data lake Parquet files. Pandas provides a comprehensive `read_*` and `to_*` I/O subsystem with parameters to optimize parsing speed, handle date parsing, and enforce data schemas.
- Ingest flat files with parameters: `sep`, `usecols`, `dtype`, `na_values`, `parse_dates`, and `chunksize`.
- Read and write multi-worksheet Excel workbooks using `openpyxl`.
- Query relational databases using SQLAlchemy connection engines with `pd.read_sql()`.
- Adopt Apache Parquet format for 10x smaller file sizes and 20x faster read speeds.
Industry Standard
Standard: Modern data lake architectures at Uber and Netflix mandate storing analytical datasets in columnar compressed Apache Parquet (`snappy` compression) instead of raw CSVs.
Real-World Example: ScoopCraft Multi-Format Data Pipeline
ScoopCraft ingests POS transactions from CSV, financial budgets from Excel, and writes clean data to Parquet.
| Format | File / Connection | Pandas Method | Benchmark Read Speed |
|---|---|---|---|
| CSV Flat File | daily_pos_sales.csv (250MB) | pd.read_csv() | 4.2 seconds |
| Excel Workbook | financial_targets_2026.xlsx | pd.read_excel(sheet_name="Q1") | 8.5 seconds |
| PostgreSQL DB | postgresql://user:pass@host/db | pd.read_sql("SELECT * ...") | 3.1 seconds |
| Apache Parquet | sales_warehouse.parquet (25MB) | pd.read_parquet() | 0.18 seconds (23x faster!) |
Syntax & Core Code Implementation
import pandas as pd
from sqlalchemy import create_engine
# 1. High-Performance CSV Ingestion
df_csv = pd.read_csv(
"zepto_orders_2026.csv",
usecols=["order_id", "store_id", "order_total", "order_date"],
dtype={"store_id": "category", "order_total": "float32"},
parse_dates=["order_date"],
na_values=["N/A", "missing", "NULL"]
)
# 2. Excel multi-sheet read & write
df_excel = pd.read_excel("targets.xlsx", sheet_name="Bengaluru_Outlets")
# 3. SQL Database Connection with SQLAlchemy
engine = create_engine("postgresql+psycopg2://admin:secret@localhost:5432/pos_db")
df_sql = pd.read_sql("SELECT * FROM fact_orders WHERE status = 'Delivered'", con=engine)
# 4. Exporting to high-performance Apache Parquet
df_csv.to_parquet("clean_orders.parquet", engine="pyarrow", compression="snappy")
Parameters & Arguments Breakdown
usecols: List of column names or callable to ingest, drastically reducing memory usage by ignoring unused columns.parse_dates: List of column names to parse directly as datetime64 objects during ingestion.chunksize: Integer row count; returns an iterable reader yielding chunks to process files larger than RAM.to_parquet(): Writes DataFrame to Apache Parquet columnar binary format with snappy/gzip compression.
Sample Walkthrough (Q&A)
Question: How do you process a massive 20GB CSV file on a laptop with only 8GB of RAM using Pandas?
Code Pattern: for chunk in pd.read_csv("huge.csv", chunksize=100_000): process(chunk)
Result: Streams file in manageable 100,000-row chunks, keeping RAM usage below 500MB.
Explanation: Setting `chunksize` returns a TextFileReader generator. You aggregate or filter each batch iteratively without loading the entire 20GB into memory at once.
Common Mistakes & Limitations
- Reading entire CSV files into memory without specifying `usecols`, causing out-of-memory crashes on large production datasets.
- Writing DataFrames to CSV with `df.to_csv("file.csv")` without `index=False`, which creates an unwanted, unnamed index column in the exported file.
Keyboard Shortcuts (Jupyter & VS Code)
| Action | Windows | Mac |
|---|---|---|
| Export without Unwanted Index | df.to_csv("out.csv", index=False) | df.to_csv("out.csv", index=False) |
| Fast Parquet Export | df.to_parquet("out.parquet") | df.to_parquet("out.parquet") |
Pro Tips & Modern Best Practices
- Use `engine="pyarrow"` in `pd.read_csv(..., engine="pyarrow")` for multi-threaded C++ CSV ingestion up to 5x faster than standard C parser.
- Use DuckDB to directly query remote CSV or Parquet files via S3 without loading into Pandas.