export dataframe as csv file in python

To export a Pandas DataFrame as a CSV file in Python, you can use the to_csv() method. Here's an example code snippet:

main.py
import pandas as pd

# Create a sample DataFrame
data = {
    'col1': [1, 2, 3],
    'col2': ['A', 'B', 'C'],
    'col3': [0.1, 0.2, 0.3]
}
df = pd.DataFrame(data)

# Export DataFrame to a CSV file
df.to_csv('my_dataframe.csv', index=False)
241 chars
13 lines

In the above example, we first create a sample DataFrame using a dictionary object. Then we export this DataFrame into a CSV file named my_dataframe.csv using the to_csv() method.

The index=False parameter is used to exclude index from the output CSV as we have not set an index in our dataframe.

gistlibby LogSnag