By the end of this lesson, you will be able to:
pd.read_csv and control parsing through key parameters (sep, encoding, parse_dates, index_col)pd.json_normalizepd.read_sqldtype optimization⏱️ Estimated Time: 45–60 minutes
🎯 Project: Build a "morning data pipeline" that pulls a product catalog from Excel, daily orders from CSV, customers from a SQL database, and shipping status from a JSON API, then combines them into one dataset.
Master the art of importing data from various sources into pandas DataFrames
Think of pandas as a universal translator for data formats. Just like a skilled interpreter who can understand multiple languages and convert between them seamlessly, pandas can read data from virtually any source - CSV files, Excel spreadsheets, JSON documents, SQL databases, and more!
In the real world, data rarely comes in a single, perfect format. You might receive sales data in Excel, customer feedback in CSV, API responses in JSON, and transaction records in a SQL database. Pandas is your Swiss Army knife for bringing all this data together.
The bread and butter of data exchange - simple, universal, and human-readable
# Basic CSV loading
df = pd.read_csv('sales_data.csv')
# With custom settings
df = pd.read_csv('data.csv',
sep=';', # European CSV with semicolon
encoding='utf-8', # Handle special characters
parse_dates=['date_column'], # Auto-parse dates
index_col='id' # Use 'id' column as index
)
Corporate data's favorite format - multiple sheets, formatting, and formulas
# Read specific sheet
df = pd.read_excel('report.xlsx', sheet_name='Q1_Sales')
# Read multiple sheets
all_sheets = pd.read_excel('report.xlsx', sheet_name=None)
# Returns dictionary: {'Sheet1': df1, 'Sheet2': df2, ...}
# Skip rows and use specific columns
df = pd.read_excel('data.xlsx',
skiprows=2, # Skip header rows
usecols='A:E', # Only columns A to E
na_values=['N/A', 'Missing'] # Custom NA values
)
The language of web APIs - nested, hierarchical, and flexible
# Simple JSON
df = pd.read_json('api_response.json')
# Nested JSON with normalization
import json
with open('nested_data.json') as f:
data = json.load(f)
df = pd.json_normalize(data,
record_path=['results'], # Path to records
meta=['timestamp', 'version'] # Include metadata
)
# From API endpoint
df = pd.read_json('https://api.example.com/data')
Enterprise data warehouses - structured, relational, and massive
# Using SQLAlchemy
from sqlalchemy import create_engine
# Connect to database
engine = create_engine('sqlite:///database.db')
# Or: 'postgresql://user:pass@host/db'
# Or: 'mysql://user:pass@host/db'
# Read entire table
df = pd.read_sql_table('customers', engine)
# Custom SQL query
query = """
SELECT customer_id, order_date, total
FROM orders
WHERE order_date >= '2024-01-01'
ORDER BY total DESC
"""
df = pd.read_sql_query(query, engine)
Watch how different file formats are loaded and processed:
When dealing with large files, use these techniques to manage memory:
# Read in chunks
chunk_size = 10000
for chunk in pd.read_csv('huge_file.csv', chunksize=chunk_size):
# Process each chunk
processed = chunk.groupby('category').sum()
# Save or aggregate results
# Specify data types to reduce memory
dtypes = {
'product_id': 'category', # Categorical for repeated values
'quantity': 'int32', # Smaller integer type
'price': 'float32' # Single precision float
}
df = pd.read_csv('data.csv', dtype=dtypes)
# Only load specific columns
df = pd.read_csv('data.csv', usecols=['col1', 'col2', 'col3'])
encoding='utf-8' or 'latin-1' for special charactersparse_datesna_valuesImagine you're a data analyst at an e-commerce company. You need to combine data from multiple sources:
# Morning routine: Load yesterday's data from various sources
import pandas as pd
from datetime import datetime, timedelta
from sqlalchemy import create_engine
# 1. Load product catalog from Excel (updated weekly)
products = pd.read_excel('product_catalog.xlsx',
sheet_name='Active_Products')
# 2. Load customer orders from CSV (daily export from order system)
yesterday = (datetime.now() - timedelta(1)).strftime('%Y%m%d')
orders = pd.read_csv(f'orders_{yesterday}.csv',
parse_dates=['order_timestamp'],
dtype={'customer_id': str}) # Keep as string to preserve leading zeros
# 3. Load customer data from SQL database
engine = create_engine('postgresql://analyst:pass@datawarehouse/ecommerce')
customers = pd.read_sql_query("""
SELECT customer_id, segment, lifetime_value, registration_date
FROM customers
WHERE status = 'active'
""", engine)
# 4. Load shipping data from JSON API response
shipping = pd.read_json('https://api.shipping.com/tracking/bulk')
# 5. Combine all data sources
# Merge orders with products
order_details = orders.merge(products, on='product_id', how='left')
# Add customer information
full_data = order_details.merge(customers, on='customer_id', how='left')
# Add shipping status
final_dataset = full_data.merge(shipping, on='order_id', how='left')
print(f"Combined dataset: {final_dataset.shape[0]} rows, {final_dataset.shape[1]} columns")
print(f"Data sources integrated successfully!")
df.head(), df.info(), and df.describe().gz, .zip, .bz2)%%time in Jupyter to measure loading performanceKeep a learning journal — digital or physical. After this lesson, take a few minutes to write down:
✍️ This lesson's prompt: Data rarely arrives in one tidy format. Which source type (CSV, Excel, JSON, or SQL) do you expect to work with most, and what one parameter or option from this lesson do you most want to remember when you load it?
read_* function for each format — read_csv, read_excel, read_json, read_sql, read_html — that all return a DataFrame.head(), info(), and describe() immediately after loading.You can now pull data into pandas from CSV, Excel, JSON, and SQL sources, tune how it's parsed, and combine several sources into a single working dataset — the essential first step of every analysis.
That's almost always an encoding mismatch. Try encoding='utf-8', and if that fails, encoding='latin-1'. Files exported from Excel on Windows sometimes need encoding='cp1252'.
Pandas infers those columns as integers and drops the zeros. Force them to stay as text with dtype={'id_column': str} when you read the file.
Read it in pieces with chunksize and aggregate each chunk, load only the columns you need with usecols, or downcast numeric columns to smaller dtypes. For truly massive data, tools like Dask extend the pandas API.
Loading is only step one — real datasets arrive messy. Next you'll learn to detect and fix missing values, duplicates, wrong types, and outliers so your data is ready for analysis.
parse_dates and a custom dtype map, then check the result with df.info().Every great analysis starts with getting the data in the door. You now hold the keys to almost any data source you'll meet — that's a genuinely powerful skill. Onward!