Save pandas to CSV
df.to_csv() code
generated live.
Set your filename, separator, encoding and options — the df.to_csv() call updates live. All parameters explained: index, sep, encoding, float_format, na_rep, chunked writing for large files. No data leaves your browser.
to_csv() parameters — complete reference
Every parameter you'll actually use, with the default value and when to change it.
| Parameter | Default | When to change it |
|---|---|---|
| index | True | Set index=False in almost every case. Without it, pandas writes the integer row numbers (0, 1, 2…) as the first column of the CSV, which is rarely wanted. |
| sep | ',' | Change to '\t' for TSV, ';' for European systems where comma is the decimal separator. Use '|' for data containing commas in text fields. |
| encoding | 'utf-8' | Use 'utf-8-sig' when the file will be opened in Excel on Windows — the BOM tells Excel to decode as UTF-8. Use 'latin-1' for legacy systems that don't accept UTF-8. |
| float_format | None | Use '%.2f' to round all floats to 2 decimal places in the output. Without this, pandas writes full floating-point precision (e.g. 0.30000000000000004). |
| na_rep | '' | The string written for NaN/NaT values. Change to 'NA' or 'NULL' when the receiving system requires explicit null markers. |
| columns | None | List of column names to include. Use to select a subset: columns=['name','age','email']. Columns appear in the order listed. |
| header | True | Set header=False to omit the column name row — useful when appending to an existing CSV in a loop. |
| mode | 'w' | Set mode='a' to append rows to an existing file. Always pair with header=False when appending to avoid duplicate headers. |
| chunksize | None | Number of rows to write at a time. Use for DataFrames with millions of rows to reduce peak memory usage during serialisation. |
| date_format | None | Format string for datetime columns: '%Y-%m-%d' for ISO dates, '%d/%m/%Y' for UK format. Without this, datetimes write with full timestamp precision. |
| quoting | csv.QUOTE_MINIMAL | csv.QUOTE_ALL wraps every field in quotes. csv.QUOTE_NONNUMERIC quotes all non-numeric fields. Import csv module to use these constants. |
Common patterns
The most useful to_csv() recipes beyond the basic call.
When do you need pandas to CSV?
ETL pipeline output
Data transformation pipelines built in pandas — cleaning, merging, aggregating — typically write their output to CSV for handoff to downstream systems. to_csv() is the final step of almost every pandas ETL script.
Excel-compatible export
Business users opening the CSV in Excel on Windows need encoding='utf-8-sig' to see accented characters correctly, and float_format='%.2f' to avoid floating-point noise in numeric columns.
Database staging
Exporting a cleaned DataFrame to CSV for bulk loading into PostgreSQL, MySQL or Snowflake. The na_rep and date_format parameters ensure nulls and dates match the database's expected format.
Incremental export
Scheduled pandas jobs that append new rows to a running CSV — daily exports, log aggregations, monitoring data. The mode='a' + header=False pattern appends without duplicating the header row.
Related CSV tools
Every to_csv() parameter,
explained and generated. Not just index=False.
Most pandas to CSV guides show one line: df.to_csv('output.csv', index=False). CSVShift covers the eleven parameters you'll actually need — encoding for Excel compatibility, float_format to avoid precision noise, na_rep for database staging, mode='a' for incremental appends, chunksize for large files.
The live generator produces the exact call for your configuration. No manual concatenation of parameters, no checking the docs for which keyword is float_format vs float_precision.
11 parameters covered
Live to_csv() builder
Four code patterns
Free, no conditions
How do I save a pandas DataFrame to CSV?
df.to_csv('output.csv', index=False). The index=False prevents the integer row numbers from becoming a column in the file. For Excel compatibility on Windows, use encoding='utf-8-sig'.Why does pandas to_csv write an extra first column?
index=True, which writes the row numbers (0, 1, 2…) as the first column. Fix: df.to_csv('output.csv', index=False). If the index contains meaningful data (e.g. dates from a time series), use df.reset_index() first to move it to a regular column.Why does my pandas CSV show garbled characters in Excel?
encoding='utf-8-sig' instead of 'utf-8'. The utf-8-sig encoding adds a BOM (Byte Order Mark) that Excel on Windows uses to detect UTF-8 automatically. Without it, Excel interprets the file as Windows-1252 and garbles any non-ASCII character (accented letters, currency symbols, emoji).How do I append rows to an existing CSV with pandas?
df.to_csv('output.csv', mode='a', header=False, index=False). The header=False is critical — without it, pandas writes the column names again on every append, creating duplicate header rows in the file. Check if the file exists first with os.path.isfile() to write the header only on the first call.How do I fix floating-point precision in pandas CSV output?
float_format='%.2f' to limit all float columns to 2 decimal places. Without this, pandas writes full IEEE 754 precision: a value stored as 0.3 may write as 0.30000000000000004. The format string follows Python's % formatting: %.4f for 4 places, %g for compact notation.