Sub topic 3
Controlling the CSV output
This chapter assumes you can build a DataFrame and have already written one with to_csv. Here you will change what ends up in the file: the index, the header, the separator, missing values, and number and date formats.
Start with a sample DataFrame
The examples below reuse this DataFrame.
import pandas as pd
df = pd.DataFrame({
"order_id": [1, 2, 3],
"customer": ["Ana", "Ben", None],
"amount": [19.99, 5.5, None],
"ordered_at": pd.to_datetime(["2026-01-05", "2026-01-06", "2026-01-07"]),
})Index and header
By default to_csv writes the row index as an unnamed first column. When you read the file back, that column returns as Unnamed: 0. Pass index=False unless the index carries real data.
index=Falsedrops the index column.index_label="id"names the index column when you do keep it.header=Falseomits the column names line.header=["id", "name", "total", "date"]writes different names without changing the DataFrame.columns=["order_id", "amount"]selects and orders the columns to write.
df.to_csv("orders_subset.csv", index=False, columns=["order_id", "amount"])Separator, decimal mark, and missing values
Many European spreadsheet settings expect ; between fields, because , is the decimal mark. The sep and decimal arguments handle this. By default, missing values become empty fields. The na_rep argument writes an explicit marker instead.
df.to_csv(
"orders_eu.csv",
index=False,
sep=";",
decimal=",",
na_rep="NA",
float_format="%.2f",
date_format="%Y-%m-%d",
encoding="utf-8",
)The file contains:
order_id;customer;amount;ordered_at
1;Ana;19,99;2026-01-05
2;Ben;5,50;2026-01-06
3;NA;NA;2026-01-07What each formatting option touches
| Argument | Affects | Notes |
|---|---|---|
| `sep` | Field delimiter | Must be a single character. |
| `decimal` | Float columns | Only the decimal mark changes. There are no thousands separators. |
| `na_rep` | Missing values | Applies to every column, including text. |
| `float_format` | Float columns | A `%` format string, such as `"%.2f"`. |
| `date_format` | Datetime columns | A `strftime` pattern. |
float_format applies only to float columns, so integer columns such as order_id are unchanged. It also rounds the value in the file only. The DataFrame keeps its full precision.
flowchart LRCommon errors
- Extra
Unnamed: 0column on reload. You forgotindex=Falsewhen writing. - A spreadsheet shows everything in one column. The program expects a different separator than the one you wrote. Match
septo the reader's locale. - Numbers read back as text. You wrote
decimal=","but read the file with the defaultdecimal=".". Pass the samedecimalandseptoread_csv. - A real
"NA"string reads as missing.read_csvtreatsNAas missing by default. IfNAcan be a genuine value, choose a different marker, or setkeep_default_na=Falsewhen reading.
Reading the file back
Use matching arguments when you read the European file.
back = pd.read_csv(
"orders_eu.csv",
sep=";",
decimal=",",
parse_dates=["ordered_at"],
)
print(back)Exercise
You write a DataFrame with df.to_csv("out.csv", sep=";", decimal=","). A colleague reads it with pd.read_csv("out.csv") and finds that the amount column is text. Which arguments fix the read, and why?
Show answer
Pass sep=";" and decimal="," to read_csv. With the default comma separator, the file's fields are not split correctly. With the default decimal=".", a value such as 19,99 cannot be parsed as a float, so the column stays as text.
Exercise
Write df to orders_report.csv with no index, the missing-value marker -, and dates shown as 05/01/2026.
Show answer
df.to_csv("orders_report.csv", index=False, na_rep="-", date_format="%d/%m/%Y")