Read a CSV File with pandas read_csv
Load comma-separated data into a DataFrame in one line, from a file, a URL or a string.
- pd.read_csv with a path or StringIO
- Header rows and column types
- Useful options: sep, usecols, parse_dates
- 1
name,scoreHeader → column names - 2
Ada,95Each line → a row - 3
pd.read_csv(...)A DataFrame with 3 rows
When is pandas read_csv used?
The problem
Most data you will meet is stored in CSV files: exports from spreadsheets, bank statements, sensor logs. Splitting lines and commas by hand breaks on quotes, missing values and numbers stored as text. pandas reads them correctly in one line.
Think of it like this
read_csv is like a data-entry clerk copying a list into a spreadsheet. The first line gives the column titles, every following line becomes one row, and the clerk notices which columns hold numbers.
"name,score\n..."📃 The lines of the CSV textStringIO(csv_text)📂 Pretend the text is an open filepd.read_csv(...)🧑💼 Copy it into a tablestudents📊 Columns name and score, three rowspd.read_csv reads comma-separated text from a file or file-like object into a DataFrame, using the first line as column names and guessing each column's type.
CSV commonly moves data between spreadsheets, business systems, analytics tools, APIs, and databases. pandas.read_csv() converts it into a DataFrame for cleaning, filtering, analysis, and visualization.
Common use cases
- Analyze exports from Excel, Google Sheets, or a CRM.
- Load datasets for machine learning and data science.
- Clean missing, incorrectly typed, or duplicated data before import.
When should you use it?
- Loading exported data from spreadsheets, databases and websites.
- Reading logs and reports in bulk.
- The first step of almost every data analysis.
When not to use it
- Excel files with several sheets: use
pd.read_excel. - Files too large for memory: read them in chunks with
chunksize.
Basic example
import pandas as pd
from io import StringIO
csv_text = "name,score\nAda,95\nLin,88\nSam,91"
students = pd.read_csv(StringIO(csv_text))
print(students)
Result of running the code
name score
0 Ada 95
1 Lin 88
2 Sam 91
Advanced example
Real CSV files are often large and imperfect. This example selects required columns, parses dates, controls data types, and recognizes several missing-value formats.
import pandas as pd
sales = pd.read_csv(
"sales.csv",
usecols=["order_date", "product", "quantity", "revenue"],
parse_dates=["order_date"],
dtype={"product": "string", "quantity": "Int64"},
na_values=["", "N/A", "unknown"],
)
sales = sales.dropna(subset=["order_date", "product"])
monthly_revenue = sales.groupby(
sales["order_date"].dt.to_period("M")
)["revenue"].sum()
print(monthly_revenue)
Code explanation
csv_text contains three data lines below a header line. StringIO wraps that text so read_csv can read it as if it were a file on disk. read_csv uses the header for the column names, turns each following line into a row and recognizes that score contains whole numbers.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Add rows, columns, and missing values.
- Combine filtering, sorting, and groupby into a small report.
- Load a real file, inspect dtypes, and handle invalid data.
Common mistakes
Wrong separator
European CSV files often use ; so everything ends up in one column.
pd.read_csv("data.csv")
pd.read_csv("data.csv", sep=";")
Finished this lesson?
Mark it as learned to track your progress.