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 clunky to first convert the Series to a DataFrame simply for a simple I/O:
import numpy as np
import pandas as pd
s = pd.Series([1,3,5,np.nan,6,8])
pd.DataFrame(s).to_excel('s.xlsx', 's')
Use pandas to_excel() function to write a DataFrame to an excel sheet with extension . xlsx. By default it writes a single DataFrame to an excel file, you can also write multiple sheets by using an ExcelWriter object with a target file name, and sheet name to write to.
How to use the tolist() method to convert pandas series to list. To convert a pandas Series to a list, simply call the tolist() method on the series which you wish to convert.
You can either:
DataFrame
from the start,in which case you've already answered your own question.
Series.to_frame()
s.to_frame(name='column_name').to_excel('xlfile.xlsx', sheet_name='s')
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 /
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With