Skip to content Skip to sidebar Skip to footer

Pandas Series To Excel

The pandas.Series object does have many to_* functions, yet it lacks a to_excel function. Is there an easier/better way to accomplish the export in line 3 of this snippet? It feels

Solution 1:

You can either:

1. construct a DataFrame from the start,

in which case you've already answered your own question.

2. Use Series.to_frame()

s.to_frame(name='column_name').to_excel('xlfile.xlsx', sheet_name='s')

Solution 2:

New in 0.20: Series.to_excel()

Beginning with pandas version 0.20, Series now supports to_excel directly (see PR #8825 for details):

import pandas as pd
s = pd.Series([0, 1, 2, 4, 8, 16], name='a_series')
s.to_excel('foo.xlsx')

Contents of file foo.xlsx:

  |  A | B         | 
--+----+-----------+---------------------
1 |    | a_series  | 
2 |  0 | 0         | 
3 |  1 | 1         | 
4 |  2 | 2         | 
5 |  3 | 4         | 
6 |  4 | 8         | 
7 |  5 | 16        | 
-.           ,---------------------------
  \ Sheet 1 /  \ Sheet 2 / \ Sheet 3 /

Post a Comment for "Pandas Series To Excel"