Lesson 20 of 60 · python
Writing Data: Exporting DataFrames
Duration: 15 minutes
Exporting DataFrames
After processing, you often need to save results.
To CSV
df.to_csv('output/cleaned.csv', index=False)
To Excel
df.to_excel('output/report.xlsx', sheet_name='Summary', index=False)
To JSON
df.to_json('output/data.json', orient='records', lines=True)
To SQL (using SQLAlchemy)
from sqlalchemy import create_engine
engine = create_engine('sqlite:///mydb.sqlite')
# Write DataFrame to a table called "sales"
df.to_sql('sales', con=engine, if_exists='replace', index=False)
Compression
df.to_csv('output/compressed.csv.gz', compression='gzip')
df.to_parquet('output/data.parquet')
```</Alert>