The Wayback Machine - https://web.archive.org/web/20240708193101/https://www.geeksforgeeks.org/saving-a-pandas-dataframe-as-a-csv/
Open In App

Saving a Pandas Dataframe as a CSV

Last Updated : 03 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how we can export a Pandas DataFrame to a CSV file by using the Pandas to_csv() method. By default, the to csv() method exports DataFrame to a CSV file with row index as the first column and comma as the delimiter. 

Creating DataFrame to Export Pandas DataFrame to CSV

Python3




# importing pandas as pd
import pandas as pd
 
# list of name, degree, score
nme = ["aparna", "pankaj", "sudhir", "Geeku"]
deg = ["MBA", "BCA", "M.Tech", "MBA"]
scr = [90, 40, 80, 98]
 
# dictionary of lists
dict = {'name': nme, 'degree': deg, 'score': scr}
     
df = pd.DataFrame(dict)
 
print(df)


Output:

     name  degree  score
0  aparna     MBA     90
1  pankaj     BCA     40
2  sudhir  M.Tech     80
3   Geeku     MBA     98

Export CSV to a working directory

Here, we simply export a Dataframe to a CSV file using df.to_csv().

Python3




# saving the dataframe
df.to_csv('file1.csv')


Output:

Saving a Pandas Dataframe as a CSV

 

   

Saving CSV without headers and index. 

Here, we are saving the file with no header and no index number.

Python3




# saving the dataframe
df.to_csv('file2.csv', header=False, index=False)


Output:

Saving a Pandas Dataframe as a CSV

 

   

Save the CSV file to a specified location

We can also, save our file at some specific location.

Python3




# saving the dataframe
df.to_csv(r'C:\Users\Admin\Desktop\file3.csv')


Output:

 

Write a DataFrame to CSV file using tab separator

We can also save our file with some specific separate as we want. i.e, “\t” .

Python3




import pandas as pd
import numpy as np
 
users = {'Name': ['Amit', 'Cody', 'Drew'],
    'Age': [20,21,25]}
 
#create DataFrame
df = pd.DataFrame(users, columns=['Name','Age'])
 
print("Original DataFrame:")
print(df)
print('Data from Users.csv:')
 
df.to_csv('Users.csv', sep='\t', index=False,header=True)
new_df = pd.read_csv('Users.csv')
 
print(new_df)


Output:

Original DataFrame:
   Name  Age
0  Amit   20
1  Cody   21
2  Drew   25
Data from Users.csv:
  Name\tAge
0  Amit\t20
1  Cody\t21
2  Drew\t25


Previous Article
Next Article

Similar Reads

How to Remove Index Column While Saving CSV in Pandas
In this article, we'll discuss how to avoid pandas creating an index in a saved CSV file. Pandas is a library in Python where one can work with data. While working with Pandas, you may need to save a DataFrame to a CSV file. The Pandas library includes an index column in the output CSV file by default. Further in the article, we'll understand the d
3 min read
Saving Text, JSON, and CSV to a File in Python
Python allows users to handle files (read, write, save and delete files and many more). Because of Python, it is very easy for us to save multiple file formats. Python has in-built functions to save multiple file formats. Opening a text file in Python Opening a file refers to getting the file ready either for reading or for writing. This can be don
5 min read
Scrape IMDB movie rating and details using Python and saving the details of top movies to .csv file
We can scrape the IMDb movie ratings and their details with the help of the BeautifulSoup library of Python. Modules Needed: Below is the list of modules required to scrape from IMDB. requests: Requests library is an integral part of Python for making HTTP requests to a specified URL. Whether it be REST APIs or Web Scraping, requests must be learne
4 min read
Saving scraped items to JSON and CSV file using Scrapy
In this article, we will see how to use crawling with Scrapy, and, Exporting data to JSON and CSV format. We will scrape data from a webpage, using a Scrapy spider, and export the same to two different file formats. Here we will extract from the link http://quotes.toscrape.com/tag/friendship/. This website is provided by the makers of Scrapy, for l
5 min read
How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
How to read a CSV file to a Dataframe with custom delimiter in Pandas?
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. pandas package is one of them and makes importing and analyzing data so much easier.Here, we will discuss how to load a csv file into a Dataframe. It is done using a pandas.read_csv() method. We have to import pandas library to use th
3 min read
How to export Pandas DataFrame to a CSV file?
Let us see how to export a Pandas DataFrame to a CSV file. We will be using the to_csv() function to save a DataFrame as a CSV file. DataFrame.to_csv() Syntax : to_csv(parameters) Parameters : path_or_buf : File path or object, if None is provided the result is returned as a string. sep : String of length 1. Field delimiter for the output file. na_
3 min read
Pandas - DataFrame to CSV file using tab separator
Let's see how to convert a DataFrame to a CSV file using the tab separator. We will be using the to_csv() method to save a DataFrame as a csv file. To save the DataFrame with tab separators, we have to pass "\t" as the sep parameter in the to_csv() method. Approach : Import the Pandas and Numpy modules.Create a DataFrame using the DataFrame() metho
1 min read
Export Pandas dataframe to a CSV file
Suppose you are working on a Data Science project and you tackle one of the most important tasks, i.e, Data Cleaning. After data cleaning, you don't want to lose your cleaned data frame, so you want to save your cleaned data frame as a CSV. Let us see how to export a Pandas DataFrame to a CSV file. Pandas enable us to do so with its inbuilt to_csv(
2 min read
Convert CSV to Pandas Dataframe
In this article, we will discuss how to convert CSV to Pandas Dataframe, this operation can be performed using pandas.read_csv reads a comma-separated values (csv) file into DataFrame. Example 1: In the below program we are going to convert nba.csv into a data frame and then display it. Python Code # import pandas module import pandas as pd # makin
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg