How to export Pandas DataFrame to a CSV file in Python

In this tutorial, we will learn how to export a pandas DataFrame to a CSV file in Python using a simple example.

Pandas is a powerful data science library in Python used for data manipulation and analysis by data analysts all around the world. It provides labeled data structures called dataframes which are very useful to clean and analyze data stored in CSV or excel files.

First, let us install pandas.

pip install pandas

Now, that we have installed pandas in our IDE, let us import it.

import pandas as pd

Here, we want to export a DataFrame to a CSV file of our choice. So, we will need a DataFrame first.

CSV is the most common format we use when dealing with a tabular type of data. These types of files are commonly used in data analysis projects or most often when we are working with ML projects. You would have some unstructured data that you have downloaded and processed to convert it into tabular form. One of the ways to handle this type of data in Python is by using the pandas library.

Pandas DataFrame

Pandas library of Python makes use of DataFrames to hold the data in tabular form.

Let us create one DataFrame:

# Import the pandas library
import pandas as pd

# Data of Dataframe  (a list of list with 
# each inner list representing a row)
data = [['Apple', 80], ['Mango', 50], ['Banana', 35], ['Orange',60]] 

# Create a DataFrame
df = pd.DataFrame(data,columns=['Fruit','Price'])

# Print the DataFrame
df

Output:

dataframe to csv 1

As you can see in the output above, we create a pandas DataFrame with two columns.

Comma Separated Values (CSV)

CSV file is a delimited text file that uses commas to separate values.

Now we would learn how we can export this DataFrame to a CSV file to store it, carry it, distribute it, or use it later.

See the following code with exports the DataFrame created above to a CSV file.

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

The above code creates a CSV file from the DataFrame and stores it in the current directory of the Python file. You can also give the full path of the file to it somewhere else.

Note:

  • Parameter ‘filename.csv’ is the name of the file. Change it accordingly.
  • Parameter index=False is used to store the index in the CSV file as a separate column. It is True by default.
  • There are many other parameters, refer the documentation to understand and use them according to your needs.

I hope you liked the article. Comment if you have any doubts or suggestions regarding this article.

Leave a Reply

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