

Write a Pandas DataFrame to CSV file
source link: https://thispointer.com/write-a-pandas-dataframe-to-csv-file/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Write a Pandas DataFrame to CSV file – thisPointer
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.
Advertisements
Recommend
-
61
In this article we will discuss how to get column names or Row Index names in DataFrame object. First of all, create a dataframe object of students records i.e. students = [ ('jack', 34, 'Sydeny') , ...
-
55
In this article we will discuss how to skip rows from top , bottom or at specific indicies while reading a csv file and loading contents to a Dataframe. Python panda’s library provides a function to read a csv fi...
-
59
In this article we will discuss how to sort rows in ascending and descending order based on values in a single or multiple columns . Also, how to sort columns based on values in rows using DataFrame.sort_values()
-
26
Selecting or filtering rows from a dataframe can be sometime tedious if you don’t know the exact methods and how to filter rows with multiple conditions In this post we are going to see the different ways to select...
-
10
The Pandas DataFrame is a structure that cont...
-
12
How to display full Dataframe i.e. print all rows & columns without truncation – thispointer.comIn this article we will discuss how to print a big dataframe without any truncation. Let’s create a very big dataframe with 67 rows and...
-
6
Removing duplicate records in a CSV file using Pandas · GitHub Instantly share code, notes, and snippets. Removing duplicate records in a CS...
-
13
Import a CSV file into Pandas DataFrame – thisPointer A DataFrame is a data structure that stores the data in rows and columns. In this article we will discuss how to import a csv file into a Pandas DataFrame in Python.
-
8
Saving a Pandas Dataframe as a CSVSaving a Pandas Dataframe as a CSVHello everyone. Welcome to geek for geeks. In this video. I'm going
-
7
Okay @lkwalke4, I'll take a look. Setting the Stage and Expectations Upfront, I apologize for the lengthy explanations. This response provides...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK