pandas Series to Dataframe using Series indexes as columns

后端 未结 4 1477
青春惊慌失措
青春惊慌失措 2021-02-01 14:40

I have a Series, like this:

series = pd.Series({\'a\': 1, \'b\': 2, \'c\': 3})

I want to convert it to a dataframe like this:

          


        
4条回答
  •  旧时难觅i
    2021-02-01 15:31

    You don't need the transposition step, just wrap your Series inside a list and pass it to the DataFrame constructor:

    pd.DataFrame([series])
    
       a  b  c
    0  1  2  3
    

    Alternatively, call Series.to_frame, then transpose using the shortcut .T:

    series.to_frame().T
    
       a  b  c
    0  1  2  3
    

提交回复
热议问题