How to Avoid Including Dataframe Index when Saving as CSV file in Python

To avoid including the index column when saving as a CSV file, you can add index=False in df.to_csv() to avoid including the index clumn when saving a Python Pandas dataframe as a CSV file.

df.to_csv(‘filename.csv’, index=False)

# import pandas
import pandas as pd

# create a dataframe in Python
d = {'brands': ['Tesla', 'Toyota'], 'models': ["Model 3", "RAV4"]}
df = pd.DataFrame(data=d)

# print out the dataframe
print(df)

Output:

   brands   models
0   Tesla  Model 3
1  Toyota     RAV4

The following code with index=False is not to include the column of index.

# save to a CSV without the index column
df.to_csv("Car_brands.csv",index=False)

This is how it looks like in a CSV file, which does not include the index column.

Avoid including the index column when saving a dataframe as a CSV file
Avoid including the index column when saving a dataframe as a CSV file

In contrast, if without index=False, the saved CSV file will have a column of index in the CSV file.

# save to a CSV with the index column
df.to_csv("Car_brands.csv")

This is how it looks like in a CSV file, which includes the index column.

Include the index column when saving a dataframe as a CSV file

Further Reading