The Pandas Series function to_excel() used to export the Series to the excel file. To write single object to the excel file an target file name needs to specified. It also allow to write to multiple sheets by creating an ExcelWriter object with target filename and also need to specify the sheet in the file in which we have to write.
1 Series.to_excel(excel_writer, sheet_name = 'Sheet1', na_rep = '', float_format = None, columns = None, header = True,
2 index = True, index_label = None, startrow = 0, startcol = 0, engine = None, merge_cells = True, inf_rep = 'inf',
3 freeze_panes = None, storage_options = None)
excel_writer : It can be a file path or existing ExcelWriter object.
sheet_name : It refers to the name of the sheet that contains the Series.
na_repr : It specifies the representation of missing data.
float_format : It formats the string for floating-point numbers.
columns : It refers the column to write in the excel file.
header : it specifies the column header names to write. If a list of the string is specified it considered as aliases for the column names.
index : It expects boolean value that determine whether index needs to write in the output file.
index_label : It refers to the column label for the index column. If it is not specified and the header and index value are True, then the index names are used. If Series uses MultiIndex, a sequence should be given.
startrow : It refers to the upper left cell row to dump the Series, if not specified, the default value will be 0.
startcol : It refers to the upper left cell column to dump the Series, if not specified, the default value will be 0.
engine : It specifies the engine to write the data into the excel file that can be openpyxl, or xlsxwriter.
merge_cells : It expects boolean value, if not specified the default value will be True. It writes MultiIndex and Hierarchical rows as the merged cells.
encoding : It encodes the resulting excel file. It is only necessary for the xlwt.
inf_rep : It usually represents infinity if not specified the default value will be inf.
verbose : It expects boolean value that display more information in the error logs. If not specified the default value will be True.
freeze_panes : It specifies the one based bottommost row and rightmost column that is to be frozen.
1 import pandas as pd
2 import numpy as np
3
4 ser = pd.Series([2.5, 5.3, np.nan, 2.4, np.nan, 3.0])
5
6 print('The original Series object :')
7 print(ser)
8 ser.to_excel('./output.xlsx', index = False)
9
10 res = pd.read_excel('./output.xlsx')
11 print('The Series object from Excel :')
12 print(res)
In the above example, a Series object is created by passing an array. An ExcelWriter object is created by passing output file name. A to_excel() function is called by passing writer object that write data and save file.
1 The original Series object :
2 0 2.5
3 1 5.3
4 2 NaN
5 3 2.4
6 4 NaN
7 5 3.0
8 dtype: float64
9
10 The Series object from Excel :
11 0
12 0 2.5
13 1 5.3
14 2 NaN
15 3 2.4
16 4 NaN
17 5 3.0
Note : It required module openpyxl to be installed before executing this program, to avoid error ModuleNotFoundError: No module named ‘openpyxl'.
Series with string literals
Series with string literals
1 import pandas as pd
2
3 ser = pd.Series(['Welcome', 'to', 'IOGyan'])
4
5 print('The original Series object :')
6 print(ser)
7 ser.to_excel('./output.xlsx', index = False)
8
9 res = pd.read_excel('./output.xlsx')
10 print('The Series object from Excel :')
11 print(res)
1 The original Series object :
2 0 Welcome
3 1 to
4 2 IOGyan
5 dtype: object
6
7 The Series object from Excel :
8 0 Welcome
9 1 to
10 2 IOGyan
Related options for your search