🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.to_csv()

AI & DATA SCIENCE // df-to-csv

df.to_csv() writes a DataFrame out to a CSV file, the counterpart to pd.read_csv(), controlling delimiter, index inclusion, and more.

Syntax

df.to_csv(path, sep=',', index=True)

Deep Dive Course

By default, to_csv() writes the DataFrame's index as the first column of the output file — a common surprise when reading that file back later produces an unexpected extra 'Unnamed: 0' column, since the saved index gets loaded as regular data unless you tell read_csv() to treat it as the index too. Passing index=False omits the index entirely from the output, which is usually what you want unless the index itself carries meaningful information you specifically need to preserve.

1Understanding df.to_csv()

By default, to_csv() writes the DataFrame's index as the first column of the output file — a common surprise when reading that file back later produces an unexpected extra 'Unnamed: 0' column, since the saved index gets loaded as regular data unless you tell read_csv() to treat it as the index too. Passing index=False omits the index entirely from the output, which is usually what you want unless the index itself carries meaningful information you specifically need to preserve.

💡

Pass index=False to to_csv() unless you specifically need to preserve the DataFrame's index in the output file — otherwise, reading that CSV back later commonly produces a stray extra column from the saved index.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
df.to_csv("people.csv", index=False)

with open("people.csv") as f:
    print(f.read())
localhost:3000

2Practical Example

Here is a real-world application of df.to_csv() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"]})
df.to_csv("with_index.csv")

with open("with_index.csv") as f:
    print(f.read())
localhost:3000

3Best Practices

Follow these guidelines when working with df.to_csv():

1. Pass index=False when the DataFrame's index is just a default RangeIndex with no meaningful information worth saving

2. Match sep/delimiter to whatever downstream tool will consume the file, if it's not a standard comma-separated CSV

3. Specify encoding explicitly, like encoding='utf-8', for text with non-ASCII characters, to avoid platform-dependent default-encoding issues

⚠️

Tip: Pass index=False to to_csv() unless you specifically need to preserve the DataFrame's index in the output file — otherwise, reading that CSV back later commonly produces a stray extra column from the saved index.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
df.to_csv("people.csv", index=False)

with open("people.csv") as f:
    print(f.read())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
df.to_csv("people.csv", index=False)

with open("people.csv") as f:
    print(f.read())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"]})
df.to_csv("with_index.csv")

with open("with_index.csv") as f:
    print(f.read())

Best Practices

  • Pass index=False when the DataFrame's index is just a default RangeIndex with no meaningful information worth saving
  • Match sep/delimiter to whatever downstream tool will consume the file, if it's not a standard comma-separated CSV
  • Specify encoding explicitly, like encoding='utf-8', for text with non-ASCII characters, to avoid platform-dependent default-encoding issues

Interview Question

Why might reading a CSV file back with read_csv() sometimes produce an unexpected extra column named 'Unnamed: 0'?

Hint: Think about what to_csv() saves by default, and how read_csv() interprets it.

to_csv() saves the DataFrame's index as the first column of the output file by default, even if that index is just the default, meaningless RangeIndex of row numbers. When that file is later read back with read_csv(), which doesn't know that column was originally an index, it treats it as an ordinary data column with no header name, and pandas labels an unnamed column 'Unnamed: 0'. Passing index=False when originally saving the file avoids writing that index column at all, preventing this surprise entirely.

Exercises

MediumPractice using df.to_csv() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
df.to_csv("people.csv", index=False)

with open("people.csv") as f:
    print(f.read())

Frequently Asked Questions

Why might reading a CSV file back with read_csv() sometimes produce an unexpected extra column named 'Unnamed: 0'?

to_csv() saves the DataFrame's index as the first column of the output file by default, even if that index is just the default, meaningless RangeIndex of row numbers. When that file is later read back with read_csv(), which doesn't know that column was originally an index, it treats it as an ordinary data column with no header name, and pandas labels an unnamed column 'Unnamed: 0'. Passing index=False when originally saving the file avoids writing that index column at all, preventing this surprise entirely.

Related Functions

pd-read-csvdf-to-excelpd-index