PY-401 ยท pandas basics
Reading CSV and Excel files
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
pd.read_csv("sales.csv") reads a CSV file into a DataFrame, using the first line as column names. pd.read_excel("report.xlsx") does the same for an Excel sheet. Straight after loading, check df.head(), df.shape and df.dtypes so you know what you are working with.
- Load a CSV file with read_csv
- Know how read_excel picks a sheet
- Check head, shape and dtypes after loading
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import ioimport pandas as pdcsv_text = """date,region,product,units,price2026-01-03,North,Tea,12,2.52026-01-03,South,Coffee,7,3.02026-01-04,North,Coffee,9,3.02026-01-05,East,Tea,15,2.52026-01-05,South,Cake,4,4.5"""df = pd.read_csv(io.StringIO(csv_text))print(df.head(3))print(df.shape)print(df.dtypes)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
date region product units price
0 2026-01-03 North Tea 12 2.5
1 2026-01-03 South Coffee 7 3.0
2 2026-01-04 North Coffee 9 3.0
(5, 5)
date object
region object
product object
units int64
price float64
dtype: object ๐ฏ Load csv_text into a DataFrame called df with parse_dates=["day"], then print the number of rows.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
df = pd.read_csv(io.StringIO(csv_text), parse_dates=["day"]) then print(len(df))
- The problemFinance emails a CSV export every Monday. Opening it in Excel and copying numbers takes an hour. Three lines of pandas load it, check it and get it ready for the report.
- The Python idea
df = pd.read_csv("sales.csv")pd.read_csv("sales.csv")reads a CSV file into a DataFrame, using the first line as column names. - In real lifeA bank's fraud team loads the night's card transactions from a CSV export, about 3 million rows, with
read_csvandusecolsso only the 6 columns they need fill memory.
Start with a question
Finance emails a CSV export every Monday. Opening it in Excel and copying numbers takes an hour. Three lines of pandas load it, check it and get it ready for the report.
Most data you meet at work is a file someone exported. Loading it correctly is the step every analysis depends on.
Explain it like I'm 10
read_csv is a scanner. You feed it a page with a table on it, and it turns the page into a table Python can work with, with the top line as the column names.
The technical explanation
pd.read_csv(path) reads a file. Useful options: sep=";" for semicolon files, usecols=[...] to keep some columns, parse_dates=["date"] to turn text into dates.
The example uses io.StringIO to treat a string as a file, so it runs in the browser without a real file. On your computer, pass a file name instead.
pd.read_excel("report.xlsx", sheet_name="March") reads one sheet. It needs the openpyxl package installed (pip install openpyxl).
After loading, df.head() shows the first rows, df.shape the size and df.dtypes the type pandas picked for each column.
Syntax
df = pd.read_csv("sales.csv")
df = pd.read_csv("sales.csv", parse_dates=["date"])
df = pd.read_excel("report.xlsx", sheet_name="March")
df.head(); df.shape; df.dtypes Real-world scenario
A bank's fraud team loads the night's card transactions from a CSV export, about 3 million rows, with read_csv and usecols so only the 6 columns they need fill memory.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Selecting rows and columns โQuestions people ask
How do I read only some columns from a CSV in pandas?
Pass usecols with the names you want, for example pd.read_csv("sales.csv", usecols=["date", "total"]). The other columns are never loaded, which saves memory on big files.
Why does pandas read_excel fail with "Missing optional dependency openpyxl"?
pandas uses the openpyxl package to read .xlsx files and does not install it for you. Run pip install openpyxl in the same environment and try again.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I read only some columns from a CSV in pandas?
Pass usecols with the names you want, for example pd.read_csv("sales.csv", usecols=["date", "total"]). The other columns are never loaded, which saves memory on big files.
beginner Why does pandas read_excel fail with "Missing optional dependency openpyxl"?
pandas uses the openpyxl package to read .xlsx files and does not install it for you. Run pip install openpyxl in the same environment and try again.
intermediate What does read_csv use for column names by default?
The first line of the file. The header row becomes the column names.
intermediate A date column loaded as object. What fixes it?
parse_dates=["date"]. parse_dates converts those columns to datetime.
Quick revision
Remember
- Load a CSV file with read_csv
- Know how read_excel picks a sheet
- Check head, shape and dtypes after loading
- Dates load as text (
object) unless you ask:pd.read_csv("f.csv", parse_dates=["date"]). Checkdf.dtypesbefore doing date maths.
Syntax
df = pd.read_csv("sales.csv")
df = pd.read_csv("sales.csv", parse_dates=["date"])
df = pd.read_excel("report.xlsx", sheet_name="March")
df.head(); df.shape; df.dtypes Official documentation
- pandas.read_csv (docs.python.org)
- pandas.read_excel (docs.python.org)