Load a CSV file — from your disk or straight from a URL — into a data frame.
read_csv is the front door to nearly every Pandas project, and it is far more capable than most people use it for. Getting the arguments right at load time saves you from cleaning up afterwards: the wrong dtype, dates left as strings, or columns you never needed all cost you memory and effort later.
Official documentation: pandas.read_csv
The arguments that earn their keep
pd.read_csv(source,
usecols=['a', 'b'], # read only these columns
parse_dates=['when'], # real datetimes, not strings
index_col='id', # use this column as the index
dtype={'zip': 'string'}, # stop pandas guessing
na_values=['-', 'n/a'], # what counts as missing
nrows=1000) # peek at a big file
source can be a path or a URL. Pandas fetches the URL for you, which means an example like the one below runs anywhere without downloading anything first.
I go through these in more depth in The six most important read_csv arguments in Pandas, and cover getting the index right on the way in with Load your data with the right index in Pandas.
A worked example, on real data
The USGS publishes every earthquake it records, as CSV, through a public API. No key, no signup — just a URL with a date range and a minimum magnitude. Bamboo Weekly #3 used this same source, and reached for parse_dates for exactly the reason below.
Let's find the five largest earthquakes of 2024:
import pandas as pd
url = ('https://earthquake.usgs.gov/fdsnws/event/1/query.csv'
'?starttime=2024-01-01&endtime=2024-12-31&minmagnitude=6')
(
pd.read_csv(url,
usecols=['time', 'place', 'mag', 'depth'],
parse_dates=['time'])
.sort_values('mag', ascending=False)
.head(5)
)
Which gives:
time depth mag place
2024-01-01 07:10:09.476000+00:00 10.000 7.5 2024 Noto Peninsula, Japan Earthquake
2024-07-19 01:50:48.571000+00:00 127.291 7.4 41 km ESE of San Pedro de Atacama, ...
2024-04-02 23:58:12.173000+00:00 40.000 7.4 15 km S of Hualien City, Taiwan
2024-12-17 01:47:25.741000+00:00 54.372 7.3 24 km WNW of Port-Vila, Vanuatu
2024-06-28 05:36:36.902000+00:00 24.000 7.2 10 km WSW of Atiquipa, Peru
Two arguments did real work there. usecols meant we never read the dozen columns we did not want. parse_dates meant time arrived as a datetime, so it can be sorted, filtered by range, and grouped by month — none of which works on a string that merely looks like a date.
Three mistakes people make
Leaving dates as strings. Without parse_dates, a date column is just text. Sorting it puts "10 January" before "2 February", and .dt accessors are unavailable. Pass parse_dates at load time rather than converting afterwards.
Reading the whole file, then throwing most of it away. If you only need four columns out of forty, usecols reads only those — less memory, faster load. On a large file this is the difference between comfortable and impossible. The same goes for nrows when you are still exploring and just want to see the shape of things.
Letting Pandas guess dtypes on identifier columns. Zip codes, phone numbers and account IDs look numeric, so Pandas reads them as integers — and 02134 becomes 2134. Pass dtype={'zip': 'string'} for anything that is a label rather than a quantity, even when it is made of digits.
Practice it
Work through a read_csv exercise, with instant feedback and no signup required: practice.lernerpython.com/bamboo-weekly/read-csv/
Go deeper
Bamboo Weekly is the practice. If you want the structured version — full courses with downloadable Jupyter notebooks, plus live Pandas office hours when you get stuck — that is LernerPython+Data. A paid Bamboo Weekly subscription is included with it.
See it on real data
Below are the 96 Bamboo Weekly exercises that use read_csv on real-world data — try each one, then study the worked solution.
- Bamboo Weekly #182: Surveillance technology
- Bamboo Weekly #181: Housing costs
- Bamboo Weekly #180: Movies
- Bamboo Weekly #179: Krakow tourism
- Bamboo Weekly #176: Religious restrictions
- Bamboo Weekly #175: Inflation
- Bamboo Weekly #174: Vacation
- Bamboo Weekly #172: World Cup
- Bamboo Weekly #171: Hantavirus
- Bamboo Weekly #170: Port of Long Beach
- Bamboo Weekly #169: Press freedom
- Bamboo Weekly #166: Income tax
- Bamboo Weekly #165: Artemis II
- Bamboo Weekly #164: Fertilizer
- Bamboo Weekly #162: Spotify and car accidents
- Bamboo Weekly #161: Missiles in Israel
- Bamboo Weekly #160: Strait of Hormuz
- Bamboo Weekly #158: University endowments
- Bamboo Weekly #156: Winter Olympics
- Bamboo Weekly #155: Gold
- Bamboo Weekly #153: Venezuela
- Bamboo Weekly #152: Congestion pricing
- Bamboo Weekly #149: Flu season
- Bamboo Weekly #148: US Manufacturing
- Bamboo Weekly #142: Hurricanes
- Bamboo Weekly #141: Argentina
- Bamboo Weekly #140: Stack Overflow survey
- Bamboo Weekly #138: Federal workers
- Bamboo Weekly #137: UN Security Council
- Bamboo Weekly #136: Indian vehicles
- Bamboo Weekly #134: Taiwan weather
- Bamboo Weekly #133: Wind power
- Bamboo Weekly #132: JetBrains survey
- Bamboo Weekly #131: Canadian border crossings
- Bamboo Weekly #125: Shrinking dollars
- Bamboo Weekly #121: Research funding
- Bamboo Weekly #119: Python conferences
- Bamboo Weekly #118: Flight delays
- Bamboo Weekly #117: Electricity
- Bamboo Weekly #116: Philadelphia Fed survey
- Bamboo Weekly #108: Measles
- Bamboo Weekly #107: Consumer confidence
- Bamboo Weekly #106: Flu season
- Bamboo Weekly #105: Federal employees
- Bamboo Weekly #104: Aviation accidents
- Bamboo Weekly #103: CDC data
- Bamboo Weekly #102: WordPress
- Bamboo Weekly #101: Los Angeles Fires
- Bamboo Weekly #99: Literacy and numeracy
- Bamboo Weekly #93: Anti-politics
- Bamboo Weekly #87: Nuclear power
- Bamboo Weekly #85: PACs and parties
- Bamboo Weekly #84: Central banks
- Bamboo Weekly #81: School
- Bamboo Weekly #80: Inflation
- Bamboo Weekly #78: Stock markets
- Bamboo Weekly #75: Refugees
- Bamboo Weekly #73: Avocado hand
- Bamboo Weekly #72: City travel
- Bamboo Weekly #67: Electric cars
- Bamboo Weekly #66: Pittsburgh
- Bamboo Weekly #65: Microplastics
- Bamboo Weekly #60: Iceland
- Bamboo Weekly #59: Long covid
- Bamboo Weekly #58: NATO
- Bamboo Weekly #57: International arms trade
- Bamboo Weekly #56: Rent increases
- Bamboo Weekly #55: IVF
- Bamboo Weekly #52: Border encounters
- Bamboo Weekly #51: Academy Awards
- Bamboo Weekly #50: Red Sea shipping
- Bamboo Weekly #49: Campaign finance
- Bamboo Weekly #47: Minimum wage
- Bamboo Weekly #46: Pedestrians
- Bamboo Weekly #44: Global economics
- Bamboo Weekly #43: Financial protection
- Bamboo Weekly #42: Plant hardiness
- Bamboo Weekly #39: WeWork
- Bamboo Weekly #35: Terrorism
- Bamboo Weekly #34: House of Representatives
- Bamboo Weekly #33: Fracking
- Bamboo Weekly #29: Auto accidents
- Bamboo Weekly #25: Entrepreneurship
- Bamboo Weekly #24: Wildfire smoke
- Bamboo Weekly #22: Banana index
- Bamboo Weekly #21: Electric cars
- Bamboo Weekly #18: World population
- Bamboo Weekly #15: Eurovision
- Bamboo Weekly #14: JOLTS
- Bamboo Weekly #13: Python developers
- Bamboo Weekly #11: Software jobs
- Bamboo Weekly #10: Oil prices
- Bamboo Weekly #9: US house prices
- Bamboo Weekly #7: Bank failures
- Bamboo Weekly #4: Eating well
- Bamboo Weekly #3: Earthquake
Part of the Pandas Methods Index. See also practice by skill.