Pandas How To Uncategorized How to save dataframe as csv

How to save dataframe as csv

In this post you learn how to save dataframe as csv file in Pandas library of Python. In pandas, you can save a DataFrame as a CSV file using the to_csv method.

Example of saving as csv

Here is an example:

import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'col1': [1, 2, 3, 4, 5], 'col2': [3, 4, 5, 6, 7]})

# save the dataframe as a CSV file
df.to_csv('data.csv', index=False)

In this example, data.csv is the name of the CSV file that will be created. The index parameter is set to False to exclude the index column from the file. By default, the values in the CSV file will be separated by commas, but you can specify a different separator using the sep parameter.

See also:
How to save dataframe as Excel file
How to save dataframe as text file
How to write to csv without index
How to fix read_csv() got an unexpected keyword argument ‘columns’

Tags:

Leave a Reply

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

Related Post