pandas to_csv() — live code generator — all parameters — no upload

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.

Live to_csv() generator
All parameters covered
Excel BOM encoding
Large file chunking
Always free
df.to_csv() — configure and copy  No data uploaded
Set your options — the to_csv() call updates live
df.to_csv() — ready to paste
to_csv() parameters — complete reference

Every parameter you'll actually use, with the default value and when to change it.

ParameterDefaultWhen 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.

Append rows to an existing CSV
import pandas as pd import os output_path = 'output.csv' # Write header only if file doesn't exist yet file_exists = os.path.isfile(output_path) df.to_csv( output_path, mode = 'a', # append header = not file_exists, # header only on first write index = False, encoding= 'utf-8-sig' )
Use this pattern in loops or scheduled jobs where rows are added incrementally. The os.path.isfile check prevents duplicate headers on subsequent runs.
Write large DataFrame in chunks
import pandas as pd # For DataFrames with millions of rows: df.to_csv( 'large_output.csv', index = False, encoding = 'utf-8', chunksize = 50_000 # write 50k rows at a time ) # Or split into multiple files: chunk_size = 100_000 for i, chunk in enumerate(range(0, len(df), chunk_size)): df.iloc[chunk:chunk + chunk_size].to_csv( f'output_part_{i+1}.csv', index = False ) print(f'Written part {i+1}')
chunksize reduces peak memory usage during to_csv() serialisation. Splitting into files is useful when the downstream system has row limits (Excel: 1,048,576 rows).
Save to Google Drive from Colab
from google.colab import drive, files import pandas as pd # Option 1 — save to Google Drive (persistent) drive.mount('/content/drive') df.to_csv('/content/drive/MyDrive/output.csv', index=False) # Option 2 — download to your computer df.to_csv('output.csv', index=False) files.download('output.csv')
files.download() triggers a browser download from Colab. The drive.mount() approach saves to Google Drive permanently — useful for notebooks that run on a schedule.
Save only selected columns with clean dates
import pandas as pd df.to_csv( 'clean_export.csv', columns = ['name', 'email', 'amount', 'created_at'], index = False, encoding = 'utf-8-sig', # Excel-safe UTF-8 float_format= '%.2f', # 2 decimal places date_format = '%Y-%m-%d', # ISO 8601 dates na_rep = 'NULL' # explicit nulls )
Combining float_format, date_format and na_rep produces a clean, predictable CSV regardless of the underlying DataFrame precision.
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
Popular searches
pandas to csv pandas dataframe to csv pandas to_csv pandas write to csv pandas save to csv save pandas dataframe to csv pandas export dataframe to csv to_csv pandas index false pandas to csv without index pandas to csv encoding pandas to csv utf-8 pandas append to csv

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
index, sep, encoding, float_format, na_rep, columns, header, mode, chunksize, date_format, quoting — with defaults and when to change each.
Live to_csv() builder
Six controls — filename, separator, encoding, index, float format and NA rep — generate the correct call instantly.
Four code patterns
Append mode, chunk writing, Colab export and column selection — the to_csv() recipes beyond the basic call.
Free, no conditions
No account, no upload. Funded by display advertising only.
Frequently asked questions
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?
That's the DataFrame index. By default 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?
Use 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?
Use 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.