How to write to csv without index

To write a Pandas DataFrame to a CSV file without the index, use the to_csv method and set the index parameter to False. For example this script is showing how to write to csv without index using example data and file:

import pandas as pd

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
df.to_csv('df.csv', index=False)

This will write the contents of the DataFrame df to the file df.csv without including the index.

One Reply to “How to write to csv without index”

Leave a Reply

Your email address will not be published. Required fields are marked *