Book 4 — Data Analysis with Python

Python for All

Chapter Nine — Reading and Writing CSV Files

Thanasis Troboukis  ·  All Books

Book Four · Chapter Nine

Reading and Writing CSV Files

CSV files are the standard bridge between spreadsheets and pandas. In this chapter, you will learn how to load a CSV, understand file paths, and save your results back to CSV.

What Is a CSV File?

CSV stands for comma-separated values. It is one of the simplest ways to store table data in a file. Each line is a row. Each comma separates one column from the next.

When you work with pandas on real projects, a very common workflow is: read a CSV → analyse the data → write a new CSV. That is how you move data between spreadsheets, scripts, and reports.

Path What It Means
"prices.csv" A file named prices.csv in the same folder as your script or notebook.
"data/prices.csv" A file inside a subfolder called data.
"C:/Users/Ana/data/prices.csv" or "/Users/ana/data/prices.csv" An absolute path: the full location of the file on your computer.
Keep paths simple: when you are starting out, it is easiest to save your CSV in the same folder as your script and use a path like "prices.csv".

Reading a CSV with pd.read_csv()

pd.read_csv("prices.csv") tells pandas: “open the file called prices.csv and turn it into a DataFrame.” In this playground, we create the file first so the example can run. On your own computer, the file would usually already exist.

Python · Try it — read a CSV

      

The result is a normal DataFrame. Once the file is loaded, you can use everything you already know: .head(), filtering, sorting, groupby(), new columns, and so on.

Writing a CSV with .to_csv()

To save a DataFrame to a CSV file, use .to_csv("filename.csv"). The filename is the path where you want the file to be written.

The option index=False is important. Without it, pandas also writes the row numbers as an extra first column. Most of the time, that is not what you want in a CSV you plan to share or reopen later.

Python · Try it — write a CSV

      
Rule of thumb: use index=False unless you deliberately want the row numbers saved into the CSV.

A Full CSV Workflow

Here is the complete pattern you will use in real work: load a CSV, analyse it, then save the result as a new CSV.

Python · Try it — read, analyse, write

      

Notice the flow: prices.csv comes in, pandas creates a summary table, and then that summary is written to category_summary.csv. This is how you build outputs that editors, classmates, or other scripts can reuse.

Your Turn — Save and Reload Your Own Data

Create a small price table of your own, save it to a CSV, read it back in, and then save a filtered version. Try to produce two files:

Python · Your turn

      
Congratulations — you have completed Book 4. You can now load, inspect, clean, filter, aggregate, sort, enrich, save, and reload tabular data using pandas. These are the core skills of practical data analysis. The next book covers web scraping: how to collect data automatically from websites using Python.

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…