Export Pandas DataFrame to CSV in Python

Export Pandas DataFrame to CSV in Python

The DataFrame can be exported to CSV file format using the pandas to_csv() method. As we already know, pandas is a powerful open-source library and has many useful functions that allow you to process data fast and efficiently. For any machine learning project, almost every time, you end up working with DataFrames. And, very often you need to save or export this data to a CSV file or Excel file. Also, this makes the sharing of data a lot easier.

In this post, we will explore the to_csv() function to export the DataFrame to CSV file format with an index or without index.

How to Export Pandas DataFrame to CSV?

A CSV (comma-separated values) file is a text file in which values are separated by commas. You can use the CSV file format to save data in a table structured format.

You can use pandas.DataFrame.to_csv() method to write DataFrame to a local CSV files on your system. Set the index value to “False” if you don’t want to write row names in the output CSV file.

Saving CSV file without Index

df.to_csv('fileLocation.csv', index = False)

Saving CSV file with Index


Examples to Save DataFrame to CSV

Let’s create a pandas DataFrame using dictionaries of Series. The index value is created explicitly.

# Create a DataFrame from dictionaries of Series
import pandas as pd
summaries={'AMZN': pd.Series([346.15,0.59,459,0.52,589.8,158.88],
index=['Closing price','EPS', 'Shares Outstanding(M)', 'Beta', 'P/E','Market Cap(B)']),
'GOOG': pd.Series([1133.43,36.05,335.83,0.87,31.44,380.64],
index=['Closing price','EPS','Shares Outstanding(M)', 'Beta','P/E','Market Cap(B)']),
'FB': pd.Series([61.48,0.59,2450,0.52, 104.93,150.92],
index=['Closing price','EPS','Shares Outstanding(M)', 'Beta','P/E', 'Market Cap(B)']),
'YHOO': pd.Series([34.90,1.27,1010,27.48,0.66,35.36],
index=['Closing price','EPS','Shares Outstanding(M)', 'P/E','Beta', 'Market Cap(B)'])}



                         AMZN     GOOG       FB     YHOO
Beta                     0.52     0.87     0.52     0.66
Closing price          346.15  1133.43    61.48    34.90
EPS                      0.59    36.05     0.59     1.27
Market Cap(B)          158.88   380.64   150.92    35.36
P/E                    589.80    31.44   104.93    27.48
Shares Outstanding(M)  459.00   335.83  2450.00  1010.00

Now, using the to_csv() method saves this DataFrame from the workspace to the local CSV file. By default, the index set to “True” so no need to pass when you want to write the row names to the output file.

DataFrame to CSV - WithIndex

Sometimes, the index values are not relevant and it is unnecessary to write into the CSV file. Set the index to “False” to avoid writing the row names.¬†

stock_df.to_csv('file_without_index.csv', index = False)
DataFrame to CSV - WithoutIndex


We have covered the most popular use of to_csv(), and how you can use to export DataFrame to CSV file format. Although, there are many other parameters that you can use

If you interested to learn pandas export function  to_csv() in detail then click here.

Data Analysis is one of the important parts of building a machine learning solution. And, mastering pandas will help you to achieve it efficiently. Here, curated the list of tutorials on Pandas for you.

Leave a Comment

Your email address will not be published. Required fields are marked *