Loading Data From CSV, SQL & APIs
Part of the Machine Learning Roadmap
In short
Kaggle hands you a clean CSV. Real jobs hand you a SQL database, a messy Excel export and a JSON API. Getting all three into a pandas DataFrame correctly is the unglamorous first step of every ML project.
Training data arrives as CSV or Excel files, rows in a SQL database, or JSON from an API. pandas can read all of these with read_csv, read_sql and json_normalize, so you can pull data straight into a DataFrame.

Think of it like this
Stocking the restaurant kitchen
A restaurant gets vegetables from the mandi in sacks, spices from a warehouse by order slip, and milk delivered by a vendor every morning. Different sources, different packaging, but everything ends up on the same steel counter, washed and ready. pandas is that counter: read_csv, read_sql and json_normalize unpack each delivery into the same neat DataFrame.
Watch it happen
Three sources, one DataFrame
1/5read_csv loads last year's orders export, with dates parsed and IDs kept as text.
The big ideas
What you need to know
read_csv has many knobs
Use parse_dates for dates, dtype to keep IDs and pincodes as strings, and usecols to load only what you need.
Let SQL do the filtering
pd.read_sql with a SQLAlchemy engine runs a query and returns a DataFrame. Filter and join in SQL so you pull fewer rows.
APIs return nested JSON
requests.get(...).json() gives dictionaries and lists. pd.json_normalize turns nested fields into flat columns.
Check right after loading
Look at .shape, .dtypes, .head() and .isna().sum() before doing anything else. Most bugs show up here.
See it in code
Walk through the code
1import pandas as pd2import requests3from sqlalchemy import create_engine4 5orders = pd.read_csv(6 "orders.csv",7 parse_dates=["order_date"],8 dtype={"customer_id": str, "pincode": str},9)10 11engine = create_engine("postgresql://user:password@localhost:5432/shop")12customers = pd.read_sql(13 "SELECT customer_id, city, signup_date FROM customers WHERE active = true",14 engine,15)16 17resp = requests.get("https://api.example.com/weather", params={"city": "Pune"}, timeout=10)18resp.raise_for_status()19weather = pd.json_normalize(resp.json()["days"])20 21df = orders.merge(customers, on="customer_id", how="left")22print(df.shape)23print(df.dtypes)24print(df.isna().sum())1/4Parse dates on load, and keep IDs and pincodes as text so leading zeros survive.
One CSV, one SQL table and one API call turned into DataFrames. The database URL and API are placeholders for your own.
Save yourself the pain
Common mistakes
Oops
Letting pandas read pincodes or phone numbers as integers.
Do this instead
Pass dtype={'pincode': str}. Otherwise 011001 becomes 11001 and IDs with letters break.
Oops
Running SELECT * on a huge table and filtering in pandas.
Do this instead
Filter, join and aggregate in SQL. Pull only the rows and columns the model needs.
Oops
Putting database passwords and API keys directly in the notebook.
Do this instead
Read them from environment variables or a .env file that is never committed to Git.
Your mission
Build a three-source table
Quick check
An API returns {'user': {'name': 'Asha', 'city': 'Indore'}}. Which pandas function turns this into columns user.name and user.city?
Go deeper
- 1Load the same data from a CSV and from a SQLite database with pd.read_sql.
- 2Fetch JSON from a public API with requests and flatten it with pd.json_normalize.
- 3Use dtype and parse_dates options in read_csv to get correct types on load.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.