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.
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())2Practical Example
Here is a real-world application of df.to_csv() showing how it is used in production Pandas code.
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())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.
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())