pandas Series to Dataframe using Series indexes as columns

后端 未结 4 1476
青春惊慌失措
青春惊慌失措 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条回答
  •  梦谈多话
    2021-02-01 15:25

    you can also try this:

    a = pd.Series.to_frame(series)

    a['id'] = list(a.index)

    Explanation:
    The 1st line convert the series into a single-column DataFrame.
    The 2nd line add an column to this DataFrame with the value same as the index.

提交回复
热议问题