Write a Pandas DataFrame to CSV file

In this article we will discuss how to convert pandas DataFrame to CSV File.

Table of Contents

A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method.

Let’s create a dataframe with 4 rows and 4 columns

import pandas as pd

#create dataframe for students
df=pd.DataFrame({'id':[58,59,60,61],
                 'name':['sravan','jyothika','preethi','srinadh'],
                 'age':[22,21,22,23],
                 'subjects':['java','php','sql','r/python']})

df.index.name = 'Seq'

#display dataframe
print(df)

Output:

Advertisements
     id      name  age  subjects
Seq
0    58    sravan   22      java
1    59  jyothika   21       php
2    60   preethi   22       sql
3    61   srinadh   23  r/python

We can write a pandas DataFrame to CSV file using to_csv() method. Let’s see the different ways to do this.

Write Pandas Dataframe To CSV

Here we are going to use to dataframe.to_csv() method.

Syntax:

df.to_csv(file_path, sep)  

where,

  • df is the input dataframe.
  • file_path is the File path or object, if not provided then the to_csv() returned the csv file contents as string. 
    sep is the 1 character delimeter. Default value is ‘,’.

Example: Here, we are going to save the above created dataframe to a csv file with default delimiter i.e. comma,

# Convert dataframe to csv with default separator
df.to_csv('data.csv') 

It created a file data.csv and the contents of data.csv are,

Seq,id,name,age,subjects
0,58,sravan,22,java
1,59,jyothika,21,php
2,60,preethi,22,sql
3,61,srinadh,23,r/python

We can also save the csv file in specifying the complete path instead of just file name.

Write Pandas Dataframe To CSV Without Index

Here we are going to ignore the index of the Dataframe while saving it into the csv file . We can do this by setting index parameter as False.

Syntax is as follows:

 dataframe.to_csv(file_path, sep=',', index=False) 
 

Example: Ignore the index

# Convert dataframe to csv Without the Index
df.to_csv('data.csv', index=False) 

It created a file data.csv and the contents of data.csv are,

id,name,age,subjects
58,sravan,22,java
59,jyothika,21,php
60,preethi,22,sql
61,srinadh,23,r/python

Write Pandas Dataframe To CSV Without header

Here we are going to ignore the header of the Dataframe while saving it into the csv file . We can do this by setting header parameter as False. Syntax is as follows :

dataframe.to_csv(file_path, header=False) 
 

Example: Ignore the header

# Convert dataframe to csv Without the Header
df.to_csv('data.csv', header=False) 

It created a file data.csv and the contents of data.csv are

0,58,sravan,22,java
1,59,jyothika,21,php
2,60,preethi,22,sql
3,61,srinadh,23,r/python

Write Pandas Dataframe To CSV With new Column Names

If we want to save the Dataframe to a CSV file, but with the new column names, we need to pass an header argument with a list of new column names or a bool array.

Example: Save Dataframe to CSV with different header

# Convert dataframe to csv with different Header
df.to_csv(  'data.csv',
            header=['A', 'B', 'C', 'D']) 

It created a file data.csv and the contents of data.csv are,

Seq,A,B,C,D
0,58,sravan,22,java
1,59,jyothika,21,php
2,60,preethi,22,sql
3,61,srinadh,23,r/python

Write Pandas Dataframe Specific Columns To CSV

If we want to write dataframe with specific columns to csv, then we have to specify columns in the list as a parameter.

Syntax is as follows,

df.to_csv(file_path, columns=['column1','column2',....,'column n']) 

where,

  • df is the input dataframe
  • columns are the collections of columns to be converted into csv

Example: In this example we are writing only the id, name and subjects columns to csv

# Convert dataframe to csv with specific columns only
df.to_csv(  'data.csv',
            columns=['name', 'subjects', 'id']) 

It created a file data.csv and the contents of data.csv are,

Seq,name,subjects,id
0,sravan,java,58
1,jyothika,php,59
2,preethi,sql,60
3,srinadh,r/python,61

Write Pandas Dataframe To CSV In Append Mode

We can append the data while writing a pandas dataframe to the existing CSV file. For this we need to specify the mode parameter as ‘a’.

Syntax is as follows:

dataframe.to_csv(file_path, mode='a') 

Example:

# Append the dataframe contents to an existing CSV file
df.to_csv(  'data.csv',
            mode='a') 

It created a file data.csv and the contents of data.csv are,

Seq,name,subjects,id
0,sravan,java,58
1,jyothika,php,59
2,preethi,sql,60
3,srinadh,r/python,61
Seq,id,name,age,subjects
0,58,sravan,22,java
1,59,jyothika,21,php
2,60,preethi,22,sql
3,61,srinadh,23,r/python

Setting Index Column Name In The CSV

Here we are going to set the index as the column name in csv using index_label parameter of the to_csv() function. Syntax is as follows:

dataframe.to_csv(file_path, index_label='column_name')

where

  • df is the input dataframe
  • column_name specifies the column in the dataframe for index values.

Example: We are going to specify id column name to the index values for the csv file.

# Specify ID columns for the index while
# saving Dataframe to CSV file
df.to_csv(  'data.csv',
            index_label='id') 

It created a file data.csv and the contents of data.csv are,

id,id,name,age,subjects
0,58,sravan,22,java
1,59,jyothika,21,php
2,60,preethi,22,sql
3,61,srinadh,23,r/python

Write Pandas Dataframe To Multiple CSV

Here we are going to write a pandas dataframe into multiple csv’s by splitting the rows to each csv file. We are using numpy array to split the rows and convert row by row to csv .

Example: Here we are going to write our dataframe into 4 csv files onr row each and display.

import numpy 

#split the data into 4 csv files
for i,j in enumerate(numpy.array_split(df, 4)):
    #convert each row ito csv by chunks - j
    file_name = "data_" + str(i) + ".csv"
    j.to_csv(file_name)

It created four CSV files with name data_0.csv, data_1.csv, data_2.csv and data_3.csv. Contents of the files are as,

>> cat .\data_0.csv

Seq,id,name,age,subjects
0,58,sravan,22,java

>> cat .\data_1.csv

Seq,id,name,age,subjects
1,59,jyothika,21,php

>> cat .\data_2.csv

Seq,id,name,age,subjects
2,60,preethi,22,sql

>> cat .\data_3.csv

Seq,id,name,age,subjects
3,61,srinadh,23,r/python

Summary

We discussed all the ways of writing pandas dataframe into csv file using the dataframe.to_csv() method.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top