Lesson 8 of 23
โฎPrev Nextโฏ

PY-401 ยท pandas basics

Reading CSV and Excel files

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

8of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Reading CSV and Excel files, running
    1. import io
    2. import pandas as pd
    3. csv_text = """date,region,product,units,price
    4. 2026-01-03,North,Tea,12,2.5
    5. 2026-01-03,South,Coffee,7,3.0
    6. 2026-01-04,North,Coffee,9,3.0
    7. 2026-01-05,East,Tea,15,2.5
    8. 2026-01-05,South,Cake,4,4.5
    9. """
    10. df = pd.read_csv(io.StringIO(csv_text))
    11. print(df.head(3))
    12. print(df.shape)
    13. print(df.dtypes)
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideadf = pd.read_csv("sales.csv")pd.read_csv("sales.csv") reads a CSV file into a DataFrame, using the first line as column names.
    3. In real lifeA 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.

    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

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does read_csv use for column names by default?
    2. 2. A date column loaded as object. What fixes it?
    3. 3. Which reads one sheet of an Excel file?
    4. 4. A file uses ; between values. What do you add?
    5. 5. What is the first thing to do after loading a file?

    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"]). Check df.dtypes before 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