How to write a DataFrame to a csv file

01.03.2021

Intro

It is common to manipulate and create tabular data with Pandas. Often, you will need to save this data to a csv file to send a report to other at work. In this article, we will see how to save a data frame to a csv file.

Savaing a Dataframe to a csv

To create a csv file, we use the to_csv method with a specified file path. We start by creating a dataframe, then saving the file to report.csv. This will create a csv file of our dataframe in the path we specified.

import pandas as pd


df = pd.DataFrame([
	{
		"person": "James",
		"sales": 1000,
	},
	{
		"person": "Clara",
		"sales": 3000,
	}
])

df.to_csv('./report.csv')