pandas Series to Dataframe,使用Series的索引作为列

[英]pandas Series to Dataframe, use Series' indexs as columns


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:

我想将它转换为这样的数据帧:

    a   b   c
0   1   2   3

pd.Series.to_frame does't work, it got result like,

pd.Series.to_frame不起作用,结果如下,

    0
a   1
b   2
c   3

How to construct a DataFrame from Series, with index of Series as columns?

如何从Series构建DataFrame,其中Series的索引为列?

2 个解决方案

#1


7  

You can also try this :

你也可以试试这个:

df = DataFrame(series).transpose()

Using the transpose() function you can interchange the indices and the columns. The output looks like this :

使用transpose()函数可以交换索引和列。输出如下:

    a   b   c
0   1   2   3

#2


1  

you can also try this:

你也可以试试这个:

a = pd.Series.to_frame(series)

a = pd.Series.to_frame(系列)

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

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.

说明:第1行将系列转换为单列DataFrame。第二行向此DataFrame添加一列,其值与索引相同。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/10/24/2b60338e5c61cb6f98d59e6faecd8627.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号