Sub topic 1
Which format should you save to?
Prerequisites
This chapter assumes you can run Python scripts or notebooks and build and inspect a pandas DataFrame. It also assumes you can install packages with pip and know the common column dtypes: int, float, string, and datetime.
The decision comes first
pandas can write many formats. This tutorial covers three: CSV, Excel, and Parquet. They differ in who can open the file, how large it gets, and whether column dtypes survive the trip. Pick the format before you write code, because switching later often means reworking whatever reads the file.
- CSV is plain text, with one row per line and values separated by a delimiter. Almost any tool can read it. It stores no dtypes, so every value is text until a reader guesses its type.
- Excel (
.xlsx) is a workbook with sheets, cell formatting, and formulas. It is the usual choice for people who will open the file in a spreadsheet. - Parquet is a binary, column-oriented format. It stores dtypes in the file, compresses well, and reads quickly. People can't open it in a text editor or a spreadsheet.
Comparing the three
| Property | CSV | Excel | Parquet |
|---|---|---|---|
| Human readable | Yes, in any text editor | Yes, in a spreadsheet | No |
| Keeps dtypes | No | Partly, as cell types | Yes |
| File size | Large unless compressed | Medium | Small |
| Row limit | None | 1,048,576 rows per sheet | None practical |
| Multiple tables per file | No | Yes, one per sheet | No, but a directory can hold many files |
| Extra package | None | `openpyxl` | `pyarrow` or `fastparquet` |
A decision path
The path is a starting point. Some cases change the answer:
- Another team's tool only accepts CSV. Write CSV, and document the separator, encoding, and date format.
- The data has more than about one million rows. A single Excel sheet cannot hold it, so use Parquet or CSV.
- The file is an intermediate step between two Python jobs. Use Parquet, so the next job does not have to re-parse dates and numbers.
Trying all three
The script below writes one DataFrame in each format and prints the file sizes. Install the extra packages first with pip install openpyxl pyarrow.
import os
import pandas as pd
df = pd.DataFrame({
"order_id": range(1, 10_001),
"country": ["DE", "FR", "ES", "IT"] * 2_500,
"amount": [round(i * 0.37, 2) for i in range(1, 10_001)],
"ordered_at": pd.date_range("2026-01-01", periods=10_000, freq="h"),
})
df.to_csv("sample.csv", index=False)
df.to_excel("sample.xlsx", index=False)
df.to_parquet("sample.parquet", index=False)
for path in ["sample.csv", "sample.xlsx", "sample.parquet"]:
print(path, os.path.getsize(path), "bytes")Exact sizes depend on your library versions. On data like this, Parquet is usually the smallest of the three, and CSV is the largest. Treat the numbers as a rough guide, because repetitive columns such as country compress especially well in Parquet.
Common mistakes
- Using CSV for data that must keep its types. Dates and missing values come back as plain text or guessed types, so you must restore them when reading. The CSV round trip is covered in a later chapter.
- Sending Parquet files to people who expect to open them in a spreadsheet. They can't, so export an Excel copy for them.
- Treating Excel as a data store. It is a presentation format, and its row limit and slow writes make it a poor fit for large or frequently regenerated data.
Exercise
A nightly job produces 5 million rows with datetime and nullable integer columns. A second Python job reads the result the next morning. Which format fits best, and why do the other two fall short?
Show answer
Parquet. It keeps the datetime and integer dtypes without extra read arguments, and it stays small and fast at 5 million rows. Excel cannot hold 5 million rows on one sheet because the limit is 1,048,576. CSV would work but loses the dtypes, so the second job must re-parse them.