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
Table of Contents
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.No | Parameters Description |
---|---|
1 | path_or_buf the file path or object to write the CSV data |
2 | sep field delimiter for the output CSV data. Should be a string of length 1. Default is a comma “,”. |
3 | na_rep missing or null values representation, default is an empty string. |
4 | columns the columns to write in the CSV output. |
5 | header allowed values are boolean or a list of string, default is True. If False, the column names are not written in the output. |
6 | index 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:
- Create a DataFrame from scratch
- Then export that DataFrame into a CSV file
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','PhD','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame(raw_data)
df
We have the following data about students:
first_name | degree | age | |
---|---|---|---|
0 | Sam | PhD | 25 |
1 | Ziva | MBA | 29 |
2 | Kia | PhD | 19 |
3 | Robin | MS | 21 |
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(r‘D:\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:
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','PhD','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame(raw_data)
# Export dataframe to csv file
df.to_csv(r'D:\Python\Tutorial\Example1.csv')
The csv file named Example1.csv will be saved at the above address.
Conclusion
We have covered the steps needed to create a DataFrame, and then export that DataFrame to a CSV file using to_csv function.