使用系列索引作为列的 pandas 系列到数据框 [英] pandas Series to Dataframe using Series indexes as columns

查看:44
本文介绍了使用系列索引作为列的 pandas 系列到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系列,像这样:

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 不起作用,结果如下

    0
a   1
b   2
c   3

如何从 Series 构造 DataFrame,以 Series 的索引为列?

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

推荐答案

你也可以试试这个:

df = DataFrame(series).transpose()

使用 transpose() 函数,您可以交换索引和列.输出如下所示:

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

    a   b   c
0   1   2   3

这篇关于使用系列索引作为列的 pandas 系列到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆