Convert pandas DataFrame to CSV
In this article, we will discuss how to convert pandas dataframe to csv.
Introduction
DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key
Example: Create Dataframe
In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: Dataframe is created below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
We can pandas dataframe to csv by using to_csv() function.
Syntax:
dataframe_input.to_csv('file_name.csv')
where, dataframe_input is the input dataframe and file_name.csv is the name of csv file to be converted.
There are different parameters that this method will accept. Lets see one by one as an example.
Example 1:
In this example, we are converting dataframe to csv without specifying any parameters.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#convert to csv
data.to_csv('converted_csv.csv')
Output:
Example 2:
In this example, we are converting dataframe to csv with index parameter.If it is True, it will set the indices, otherwise it will not set the indices.
This is used to set or remove the index in the csv file.
Syntax:
dataframe_input.to_csv('file_name.csv',index)
Code: Set index to False
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#convert to csv
data.to_csv('converted_csv.csv',index=False)
Output:
In the output, we will see that index will not present.
Example 3:
In this example, we are converting dataframe to csv with header parameter.
This is used to set or remove the columns in the csv file. If it is True, it will set the columns, otherwise it will not set the columns.
Syntax:
dataframe_input.to_csv('file_name.csv',header)
Code: Set header to False
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#convert to csv
data.to_csv('converted_csv.csv',header=False)
Output:
In the output, we will see that header will not present.
Example 4:
In this example, we are converting dataframe to csv with columns parameter.
This is used to set the particular columns in the csv file. We will provide the columns to be displayed in csv through a list
Syntax:
dataframe_input.to_csv('file_name.csv',columns=[columns])
Code: Set only 2 columns - 'college_id','Total Staff'
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#convert to csv
data.to_csv('converted_csv.csv',columns=['college_id','Total Staff'])
Output:
In the output, we will see that only two columns are present.
Example 5:
In this example, we are converting dataframe to csv with header parameter.
This is used to change the columns names in the csv file. We have to specify the new column names throuhg a list
Syntax:
dataframe_input.to_csv('file_name.csv',header=[new_column_names])
Code: Set columns to - ['col1','col2','col3','col3']
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#convert to csv
data.to_csv('converted_csv.csv',header=['col1','col2','col3','col3'])
Output:
In the output, we will see that column names are modified.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 12,2023