Run pandas Online

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.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • pd.read_csv with a path or StringIO
  • Header rows and column types
  • Useful options: sep, usecols, parse_dates
"name,score\nAda,95\nLin,88\nSam,91"
  1. 1name,scoreHeader → column names
  2. 2Ada,95Each line → a row
  3. 3pd.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.

From a data-entry clerk to code
"name,score\n..."📃 The lines of the CSV text
StringIO(csv_text)📂 Pretend the text is an open file
pd.read_csv(...)🧑‍💼 Copy it into a table
students📊 Columns name and score, three rows
The first line becomes the column names; each line after it becomes a row.
In one sentence

pd.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.
Tip: For production data, consider dtype, parse_dates, usecols, and na_values so parsing is explicit.

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

Run code →
main.py
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)
Get a Run button for this example

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.

advanced_csv.py
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.

  1. Add rows, columns, and missing values.
  2. Combine filtering, sorting, and groupby into a small report.
  3. Load a real file, inspect dtypes, and handle invalid data.
Run in the Python compiler →

Common mistakes

Wrong separator

European CSV files often use ; so everything ends up in one column.

Problem
pd.read_csv("data.csv")
Fix
pd.read_csv("data.csv", sep=";")

Finished this lesson?

Mark it as learned to track your progress.