Pandas dataframes are used to store and manipulate two-dimensional tabular data in python. After having performed your pre-processing or analysis with your data, you may want to save it as a separate CSV (Comma Separated Values) file for future use or reference. The pandas to_csv()
function is used to save a dataframe as a CSV file. In this tutorial, we’ll cover its usage along with some commonly used parameters through examples.
The to_csv()
function
It is a pandas dataframe function used to save a dataframe as a CSV file. The following is its syntax:
df.to_csv(path)
The above syntax by default saves the index of the dataframe as a separate column. If you do not want to include the index, pass index=False
to the above function.
Let’s create a sample dataframe first that we’ll be writing to the local machine as a CSV file.
import pandas as pd
data = {
'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',\
'Apple Inc.', 'Netflix, Inc.'],
'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'],
'Shares': [100, 50, 150, 200, 80]
}
df = pd.DataFrame(data)
df
A dataframe with name, stock symbol, and the respective shares count of companies in a sample portfolio:

Let’s see a few examples of some of the different ways to_csv()
is used:
Example 1: to_csv()
with default parameters
df.to_csv("sample_portfolio.csv")
This is how the saved CSV file looks if we open it up in Excel:

You can see in the above snapshot that using the to_csv()
function with default parameters saves it along with an additional column for index.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Example 2: to_csv()
with index=False
Generally, you may not want to include the index of the dataframe as a separate column. Particularly when they’re just continuous numbers providing no additional information. For this, you can pass the parameter index=False
to the to_csv()
function.
df.to_csv("sample_portfolio.csv", index=False)
This is how the saved CSV file looks if we open it up in Excel:

You can see in the above snapshot that the saved CSV now does not have an additional column for index.
Example 3: to_csv()
with header=False
If you do not want to include column names in your saved CSV file pass header=False
to the to_csv()
function.
df.to_csv("sample_portfolio.csv", index=False, header=False)
This is how the saved CSV file looks if we open it up in Excel:

Since we passed header=False
the saved CSV file doesn’t have the column headers. You can also pass a custom list of column names to the header
argument if you want columns to have different names. Note that we also passed index=False
.
Example 4: to_csv()
with a subset of columns
The to_csv()
function also allows you the flexibility to choose the columns you want from the dataframe to be saved to the CSV file. You can pass the column names you want to include as a list to the columns
argument.
df.to_csv("sample_portfolio.csv", index=False, columns=['Symbol', 'Shares'])
This is how the saved CSV file looks if we open it up Excel.

In the above example, we passed the list of columns to be included in the CSV file as a list to the columns
argument of the to_csv()
function. You can see that only the columns passed: Symbol
and Shares
are present in the saved CSV file. Note that we also passed index=False
.
For more on the pandas dataframe to_csv()
refer to its official documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.