how to export df to csv in python

To export a pandas dataframe to a csv file in Python, you can use the to_csv function. Here is an example:

main.py
import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df.to_csv('my_file.csv', index=False)
104 chars
4 lines

In this example, we first create a pandas dataframe df with two columns 'a' and 'b' and two rows. Then, we use the to_csv function to export the dataframe to a csv file named 'my_file.csv', without writing the index column.

By default, the to_csv function uses commas as the delimiter and writes strings in quotes. However, you can customize these options by specifying additional parameters in the function.

For more information on to_csv and other options for exporting dataframes in pandas, you can refer to the pandas documentation.

related categories

gistlibby LogSnag