pd.read_csv
pd.read_csv(path) reads a CSV file (or URL) into a DataFrame, using the first row as column names and detecting number types automatically. Use sep for other delimiters, usecols to keep only some columns, parse_dates to turn date columns into datetimes and dtype to force column types.
Syntax
pd.read_csv(filepath_or_buffer, sep=",", header=0, names=None, index_col=None, usecols=None, dtype=None, parse_dates=None, nrows=None, encoding=None)| Part | What it is |
|---|---|
filepath_or_buffer | Path, URL or file-like object to read |
sep | Delimiter, default comma; use "\t" for tab files or ";" for European CSVs |
header | Row number used as column names (0 by default); None if the file has no header |
index_col | Column name or position to use as the row index |
usecols | List of column names or positions to load; the rest is skipped |
parse_dates | List of columns to convert to datetime while reading |
Examples
import pandas as pd
df = pd.read_csv("sales.csv")
df.head(2)Result: order_id date region amount 0 1001 2024-01-05 East 120.5 1 1002 2024-01-06 West 80.0
The first row becomes the header and amount is parsed as float automatically; date stays a string unless you parse it.
df = pd.read_csv("sales.csv", parse_dates=["date"], usecols=["date", "region", "amount"])
df.dtypesResult: date datetime64[ns] region object amount float64 dtype: object
df = pd.read_csv("export.csv", sep=";", decimal=",", encoding="latin-1")Typical settings for a CSV exported from Excel in a European locale.
Common mistakes
- FileNotFoundError: the path is relative to the working directory of the Python process, not to the script, so print os.getcwd() or use an absolute path.
- Everything lands in one column: the file is not comma separated, pass sep=";" or sep="\t" (or sep=None, engine="python" to sniff it).
- UnicodeDecodeError: the file is not UTF-8, try encoding="latin-1" or encoding="cp1252".
Practice pd.read_csv
These drills check your answer on live data, free, no account needed.
- Pick two columnsFrom the `emp` table, keep only the `name` and `salary` columns, in that order. The result is a DataFrame with every row.
- Dates with to_datetime and dtThe `joined` column holds dates as text. Return a DataFrame with the columns `user`, `month` (the month number) and `weekday` (the full day name, like Monday).
Questions
How do I read a CSV with no header row?
Pass header=None so the first row is treated as data, and names=["a", "b", "c"] to give the columns names.
How do I read only some rows?
nrows=N reads the first N rows; skiprows=10 skips the first 10 lines; chunksize=100000 returns an iterator of DataFrames for files too large for memory.
Can read_csv read from a URL or a zip file?
Yes. Pass an http(s) URL directly, and a .zip or .gz path is decompressed automatically based on the extension.
Related
- to_csv and to_excelHow to export a pandas DataFrame with to_csv and to_excel: drop the index, choose separator and encoding, format floats, several sheets, append to a CSV.
- pd.to_datetimeHow to parse dates in pandas with pd.to_datetime: formats, day-first dates, coercing bad values, and extracting year, month and weekday with the dt accessor.
- astypeHow to change a column type in pandas with astype and pd.to_numeric: text to numbers, float to int, strings, categories, nullable Int64, bad values and NaN.
- head, tail and sampleHow to look at a pandas DataFrame with head, tail and sample: the first or last N rows, random rows with a seed, a fraction of rows, and showing all rows.