Export Pandas DataFrame to CSV File

write python pandas to csv

In this tutorial, we will learn to save Python Pandas DataFrame to CSV files having different formats with the help of examples.

Pandas is an open-source library built on top of NumPy library. Most of the datasets we work with are called DataFrames. DataFrame is a two dimensional labeled Data Structure with rows and columns.

You can use the following template in Python in order to export your pandas DataFrame to a csv or txt file:

df.to_csv(r'Path to save CSV file\File Name.csv', index=False)

Table of Contents

  1. Pandas DataFrame to_csv() Syntax
  2. Write Pandas DataFrame to a CSV file (Explained)

1. Pandas DataFrame to_csv() Syntax

The syntax of DataFrame to_csv() function and some of the important parameters are:

pandas.DataFrame.to_csv(path, sep, na_rep, columns, header, index)
Sr.NoParameters Description
1path_or_buf
the file path or object to write the CSV data
2sep
field delimiter for the output CSV data. Should be a string of length 1. Default is a comma “,”.
3na_rep
missing or null values representation, default is an empty string.
4columns
the columns to write in the CSV output.
5header
allowed values are boolean or a list of string, default is True. If False, the column names are not written in the output.
6index
default True. If True, the index is included in the CSV data. If False, the index value is not written in the csv output.

For complete list of to_csv parameters refer to official documentation.

Let’s review a full example:

We have the following data about students:

first_namedegreeage
0SamPhD25
1ZivaMBA29
2KiaPhD19
3RobinMS21

2. Write Pandas DataFrame to a CSV file (Explained)

Now, let’s export the DataFrame you just created to a csv file.

Step 1: Enter the path where you want to export the DataFrame as a csv file.

Step 2: Choose the file name.

Step 3: Select extension, if you want to save the file as a csv choose .csv or choose .txt if you want to save the file as a text file.

For example,

df.to_csv(rD:\Python\Tutorial\Example1.csv‘)

Notice that path is highlighted with 4 different colors:

  • The orange part represents the r character place before the pathname. It takes care of the backslash (\) character in the pathname. Backslash (\) symbol is used to escape characters that otherwise have a special meaning, such as newline, or the quote character. Hence, common errors can be avoided like: unicode error, codec can’t decode bytes in position.
  • The blue part represents the pathname where you want to save the file.
  • The green part is the name of the file you want to export.
  • The purple part represents the file type. Use ‘.csv’ to write your DataFrame to a CSV file. Alternatively, you may use the file type of ‘.txt’ to save your DataFrame to a text file.

Complete python code:

The csv file named Example1.csv will be saved at the above address.

write dataframe to csv
Exported DataFrame to csv in Microsoft Excel

Conclusion

We have covered the steps needed to create a DataFrame, and then export that DataFrame to a CSV file using to_csv function.

Leave a Comment