export pandas df as a csv file in python

To export a Pandas DataFrame as a CSV file in Python, you can use the to_csv() function provided by the Pandas library. Here is an example of how to export a DataFrame object named df as a CSV file:

main.py
import pandas as pd

# Export DataFrame as a CSV file
df.to_csv('file_name.csv', index=False)
94 chars
5 lines

In the example above, file_name.csv is the name you choose for the output CSV file. The index=False parameter is used to exclude the DataFrame index from the CSV file. If you want to include the index, you can omit this parameter or set it to True.

Make sure you have the Pandas library installed before executing this code. You can install it using pip install pandas.

related categories

gistlibby LogSnag