Pandas Series to Excel

后端 未结 2 1773
暖寄归人
暖寄归人 2021-01-02 14:13

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 th

相关标签:
2条回答
  • 2021-01-02 14:24

    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')
    
    0 讨论(0)
  • 2021-01-02 14:35

    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 /
    
    0 讨论(0)
提交回复
热议问题