What is Pandas DataFrame to excel?
The following article provides an outline for Pandas DataFrame to excel. The process of flexibly exporting all data handled in the pandas dataframe is a critical necessity. This necessity is accomplished in pandas using the to_excel() method. The to_excel() method allows to export all the contents of the dataframe into a excel sheet, on top of performing the export process it allows to make the export process with classified set of capabilities.
Syntax:
DataFrame.to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None)
Examples of Pandas DataFrame to excel
Given below are the examples mentioned:
Example #1
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame( {
'name': ['Alan Xavier', 'Annabella', 'Janawong', 'Yistien', 'Robin sheperd', 'Amalapaul', 'Nori'],
'city': ['california', 'Toronto', 'ontario', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [51, 38, 23, 64, 18, 57, 47],
'py_score': [82.0, 73.0, 81.0, 30.0, 48.0, 92.0, 84.0] })
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
Queried_Dataframe = Core_Dataframe.query(' age > 50 and py_score> 80 ')
Queried_Dataframe.to_excel(r"C:\Users\Dell\Desktop\sample.xlsx")
Output:
Explanation:
Here the dataframe is formulated with details like name, city, age and py_score. From the formulated details all rows age is greater than 50 and py_score is greater than 80 is derived as a separate dataframe and this derived dataframe is printed to the excel sheet through the to_excel() function.
Example #2
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17.2334, 22, 27],
'C' : [3, 8, 13, 18, 23.4523, 28],
'D' : [4, 9, 14, 19, 24, 29],
'E' : [5, 10, 15, 20, 25, 30]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
Core_Dataframe.to_excel(r"C:\Users\Dell\Desktop\sample.xlsx", sheet_name = 'testsheet',float_format =" %.2f " )
Output:
Explanation:
Here the pandas library is initially imported and the imported library is used for creating the dataframe which is a shape(6,6). All of the columns in the dataframe are assigned with headers which are alphabetic. The values in the dataframe are formulated in such way that they are a series of 1 to n. This dataframe is programmatically named here as core dataframe. The values of the dataframe are printed on to the excel and a snap of the excel is all placed above.
Recommended Articles
This is a guide to Pandas DataFrame to excel. Here we discuss what is Pandas DataFrame to excel? along with appropriate syntax and respective examples. The to_excel() method in pandas shows the flexibility of easily exporting the contents of the dataframe onto the excel workbook. You may also have a look at the following articles to learn more –