to_csv() (Pandas)
df.to_csv() writes a DataFrame out to a comma-separated values file or string. It is the counterpart to read_csv and the simplest way to export results for sharing or storage.
Pandas
Called with a path, to_csv writes a file; called with no path it returns the CSV as a string, which is handy for tests or piping data elsewhere. The single most important argument is index=False, which stops pandas from writing the row index as an extra unnamed first column.
You can control the output precisely: sep changes the delimiter, columns selects which columns to write, header=False omits the column names, and na_rep sets the text used for missing values. For non-ASCII text, set encoding='utf-8'.
Because CSV is plain text it loses dtype information — everything becomes a string on disk — so for round-tripping rich types many people prefer to_parquet or to_pickle.
Syntax
df.to_csv(path_or_buf=None, sep=',', index=True, columns=None, header=True)Examples
Python — index=False omits the row labels.
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.to_csv(index=False))Output:
a,b
1,3
2,4