Loading data
CSV, Excel and SQL — delimiters, encodings and parse errors.
Before you start
Data analysis starts the same way every time: pull data from somewhere and get it into a DataFrame. That "somewhere" is usually a CSV file, an Excel spreadsheet, or a database.
Each source has its own gotchas. CSVs need the right delimiter and encoding. Excel needs the right sheet. SQL needs a connection and a query. But once loaded, everything becomes the same shape — a DataFrame — and the rest of your code doesn't care where it came from.
This lesson walks through each loader, shows what the raw source looks like, what the parsed DataFrame looks like, and what breaks when you get the options wrong.
sales.csv (raw text)
commas separate fields| Row | text |
|---|---|
| h | date,region,amount |
| r1 | 2024-01-15,north,120 |
| r2 | 2024-01-16,south,80 |
DataFrame
| Row | dateobject | regionobject | amountint64 |
|---|---|---|---|
| 0 | 2024-01-15 | north | 120 |
| 1 | 2024-01-16 | south | 80 |
2 rows × 3 columns
Text in, DataFrame out. pandas split on commas and inferred int64 for amount.
Step 1 of 5. read_csv parses text into columns. A CSV file is plain text: values separated by commas, one row per line. `read_csv` splits on commas, infers types, and builds a DataFrame. The first row is assumed to be column headers. If it is not, pass `header=None` and pandas assigns 0, 1, 2 as column names.
Step 01 / 05
read_csv parses text into columns
A CSV file is plain text: values separated by commas, one row per line. read_csv splits on commas, infers types, and builds a DataFrame.
The first row is assumed to be column headers. If it is not, pass header=None and pandas assigns 0, 1, 2 as column names.
import pandas as pd# CSV — the defaultdf = pd.read_csv("sales.csv")# With optionsdf = pd.read_csv("sales.csv", sep=";", # European CSVs use semicolons encoding="latin-1", # not everything is UTF-8 na_values=["N/A", ""], # treat these as missing parse_dates=["date"], # convert to datetime)# Excel — needs openpyxldf = pd.read_excel("report.xlsx", sheet_name="Q4")# SQL — needs a connectionimport sqlite3conn = sqlite3.connect("app.db")df = pd.read_sql("SELECT * FROM orders", conn)# Inspect what you gotdf.head()df.info()df.dtypes