Writing a CSV file with pandas exports a DataFrame to delimited text that spreadsheet, database, and automation tools can import. The export choices decide whether row indexes appear as a first column, which headers are written, how missing totals are represented, and whether dates and numbers keep the format expected by the next system.
DataFrame.to_csv() writes to a path, path-like object, file-like object, or CSV string. A path-based export is the usual handoff shape because it leaves a real file that can be inspected and read back before it is sent onward.
CSV is plain text, so pandas writes the cell values it receives instead of enforcing a spreadsheet safety policy. When exported values come from users and will be opened in spreadsheet software, review formula-looking prefixes such as =, +, -, and @ before sharing the file.
Related: How to read CSV files with pandas
Related: How to write an Excel file with pandas
from pathlib import Path import pandas as pd output = Path("exports/orders.csv") output.parent.mkdir(parents=True, exist_ok=True) orders = pd.DataFrame( { "order_id": pd.Series(["A100", "A101", "A102"], dtype="string"), "customer": ["Ada Lovelace", "Lin Chen", "Maya Patel"], "region": ["EMEA", "APAC", "AMER"], "total_usd": [149.5, None, 212.0], "ordered_at": pd.to_datetime( ["2026-06-01", "2026-06-02", "2026-06-03"] ), } ) orders.to_csv( output, sep=",", index=False, columns=["order_id", "customer", "region", "total_usd", "ordered_at"], na_rep="", float_format="%.2f", date_format="%Y-%m-%d", encoding="utf-8", lineterminator="\n", ) print(f"Wrote {output}")
index=False keeps pandas row labels out of the file. columns locks the export order, and sep=“,” keeps the delimiter explicit for the receiving system.
$ python3 write_orders_csv.py Wrote exports/orders.csv
$ cat exports/orders.csv order_id,customer,region,total_usd,ordered_at A100,Ada Lovelace,EMEA,149.50,2026-06-01 A101,Lin Chen,APAC,,2026-06-02 A102,Maya Patel,AMER,212.00,2026-06-03
The header row, row count, blank total, date strings, and two-decimal totals should match the handoff target.
Tool: Comma-Separated Values (CSV) Converter
$ python3 - <<'PY'
import pandas as pd
round_trip = pd.read_csv(
"exports/orders.csv",
dtype={"order_id": "string"},
parse_dates=["ordered_at"],
)
print(round_trip.to_string(index=False))
print()
print(round_trip.dtypes.reindex(["order_id", "customer", "region", "total_usd", "ordered_at"]))
print()
print(f"Rows: {len(round_trip)}")
print(f"Columns: {', '.join(round_trip.columns)}")
print(f"Missing totals: {round_trip['total_usd'].isna().sum()}")
PY
order_id customer region total_usd ordered_at
A100 Ada Lovelace EMEA 149.5 2026-06-01
A101 Lin Chen APAC NaN 2026-06-02
A102 Maya Patel AMER 212.0 2026-06-03
order_id string
customer str
region str
total_usd float64
ordered_at datetime64[us]
dtype: object
Rows: 3
Columns: order_id, customer, region, total_usd, ordered_at
Missing totals: 1
The read-back output should show three rows, five columns, one missing total, a string order ID, and a parsed ordered_at datetime column.
$ rm -r write_orders_csv.py exports