exportdataframe to csv file in python

To export a dataframe to a CSV file in Python, you can use the to_csv() method from the Pandas library.

Here's an example that exports a dataframe to a CSV file:

main.py
import pandas as pd

# Load data into a dataframe
data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35], 'Gender': ['M', 'F', 'M']}
df = pd.DataFrame(data)

# Export dataframe to CSV file
df.to_csv('output_file.csv', index=False)
238 chars
9 lines

In this example, df.to_csv('output_file.csv', index=False) exports the dataframe df to a CSV file named 'output_file.csv' and sets index=False to omit the index column from the output.

You can customize the output further by passing additional parameters to to_csv(), such as specifying a different delimiter, quoting options, or encoding. Refer to the Pandas documentation for more information on how to use to_csv().

related categories

gistlibby LogSnag