In this tutorial, we will see how we can save pandas dataframe to CSV file (comma separated values).

Pandas to_csv – Pandas Save Dataframe to CSV file

The to_csv() method of pandas will save the data frame object as a comma-separated values file having a .csv extension.

Syntax of Pandas to_csv

The official documentation provides the syntax below, We will learn the most commonly used among these in the following sections with an example.

DataFrame.to_csv(path, sep=',', na_rep='', columns=None, header=True, index=True, index_label=None, mode='w', 
encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, date_format=None, 
doublequote=True, escapechar=None, decimal='.', errors='strict')

Let us look at some of the arguments to save the data-frame as a CSV file

1. Save dataframe to CSV file

  • path – The path of the location where the file needs to be saved which end with the name of the file having a .csv extension. If only the name of the file is provided it will be saved in the same location as the script.
  • sep – Delimiter to be used while saving the file. default is ‘,’.
  • columns – Names to the columns from the data to write in the file.
  • header – The name to be given to the columns when writing the file.
  • index – A boolean value which determines whether the column index to be included or not in the output file.
  • encoding – String representing the encoding to use in the output file, the default value is ‘utf-8’.

See the code below to save the data frame that we have created above including its column indexes and changing the names of columns.

data1.to_csv('saved_data.csv',
        header=['person_age','product_purchased'],
        sep=',',index=True)

Output:

saved_data.csv
Country,person_age,product_purchased
Germany,30,No
Spain,38,No
Germany,40,Yes
France,35,Yes

2. Save data frame to CSV file by changing separator

See the code below to save the 10 rows data frame that we have parsed from the employee dataset above but using whitespace as the separator.

import pandas as pd
url = "https://raw.githubusercontent.com/kunalgupta2616/datasets/master/employees.csv"
data2 = pd.read_csv(url,nrows=5,parse_dates=[2])
data2.to_csv('modified_emp_data.csv',sep=' ',index=False)

Output:

"First Name" Gender "Start Date" "Last Login Time" Salary "Bonus %" "Senior Management" Team
Douglas Male 1993-08-06 "12:42 PM" 97308 6.945 True Marketing
Thomas Male 1996-03-31 "6:53 AM" 61933 4.17 True 
Maria Female 1993-04-23 "11:17 AM" 130590 11.857999999999999 False Finance
Jerry Male 2005-03-04 "1:00 PM" 138705 9.34 True Finance
Larry Male 1998-01-24 "4:47 PM" 101004 1.389 True "Client Services"

For convenience output file is uploaded at GitHub whose raw file link – modified_emp_data.csv

So we learned about how we can read a CSV file and save a data frame as a CSV file along with customisations.

References:

Happy Learning 🙂