Python 将熊猫系列转换为数据帧

Python 将熊猫系列转换为数据帧,python,pandas,dataframe,series,Python,Pandas,Dataframe,Series,我有一个熊猫系列sf: email email1@email.com [1.0, 0.0, 0.0] email2@email.com [2.0, 0.0, 0.0] email3@email.com [1.0, 0.0, 0.0] email4@email.com [4.0, 0.0, 0.0] email5@email.com [1.0, 0.0, 3.0] email6@email.com [1.0, 5.0, 0.0] 我想将其转换为以下数据帧: i

我有一个熊猫系列sf:

email
email1@email.com    [1.0, 0.0, 0.0]
email2@email.com    [2.0, 0.0, 0.0]
email3@email.com    [1.0, 0.0, 0.0]
email4@email.com    [4.0, 0.0, 0.0]
email5@email.com    [1.0, 0.0, 3.0]
email6@email.com    [1.0, 5.0, 0.0]
我想将其转换为以下数据帧:

index | email             | list
_____________________________________________
0     | email1@email.com  | [1.0, 0.0, 0.0]
1     | email2@email.com  | [2.0, 0.0, 0.0]
2     | email3@email.com  | [1.0, 0.0, 0.0]
3     | email4@email.com  | [4.0, 0.0, 0.0]
4     | email5@email.com  | [1.0, 0.0, 3.0]
5     | email6@email.com  | [1.0, 5.0, 0.0]
df = df.to_frame().reset_index()

    email               0
0   email1@email.com    A
1   email2@email.com    B
2   email3@email.com    C
3   email4@email.com    D
我找到了一种方法,但我怀疑它是否更有效:

df1 = pd.DataFrame(data=sf.index, columns=['email'])
df2 = pd.DataFrame(data=sf.values, columns=['list'])
df = pd.merge(df1, df2, left_index=True, right_index=True)

您不必创建2个临时dfs,只需使用DataFrame构造函数在dict内将其作为参数传递即可:

pd.DataFrame({'email':sf.index, 'list':sf.values})
构建df的方法有很多,请参见

to_frame()

从以下系列开始,df:

email
email1@email.com    A
email2@email.com    B
email3@email.com    C
dtype: int64
我使用到_frame将序列转换为数据帧:

index | email             | list
_____________________________________________
0     | email1@email.com  | [1.0, 0.0, 0.0]
1     | email2@email.com  | [2.0, 0.0, 0.0]
2     | email3@email.com  | [1.0, 0.0, 0.0]
3     | email4@email.com  | [4.0, 0.0, 0.0]
4     | email5@email.com  | [1.0, 0.0, 3.0]
5     | email6@email.com  | [1.0, 5.0, 0.0]
df = df.to_frame().reset_index()

    email               0
0   email1@email.com    A
1   email2@email.com    B
2   email3@email.com    C
3   email4@email.com    D
现在,您只需重命名列名并命名索引列:

df = df.rename(columns= {0: 'list'})
df.index.name = 'index'
您的数据帧已准备好进行进一步分析


更新:我刚刚发现这里的答案与我的惊人的相似

一行答案是

myseries.to_frame(name='my_column_name')

带有
名称
参数
通常会出现一个用例,其中一个系列需要升级为一个数据帧。但是如果序列没有名称,那么
reset\u index
将导致如下结果:

s = pd.Series([1, 2, 3], index=['a', 'b', 'c']).rename_axis('A')
s

A
a    1
b    2
c    3
dtype: int64

您看到的列名是“0”。我们可以通过指定
名称
参数来解决此问题

s.reset_index(name='B')

   A  B
0  a  1
1  b  2
2  c  3
s.to_frame(name='B')

   B
A   
a  1
b  2
c  3


如果要创建数据帧而不将索引升级到列,请按照中的建议使用
Series.to\u frame
。这还支持名称参数

s.reset_index(name='B')

   A  B
0  a  1
1  b  2
2  c  3
s.to_frame(name='B')

   B
A   
a  1
b  2
c  3

构造函数 您还可以执行与
系列相同的操作。通过指定
参数来设置帧

pd.DataFrame(s, columns=['B'])

   B
A   
a  1
b  2
c  3
可用于将
系列
转换为
数据帧

# The provided name (columnName) will substitute the series name
df = series.to_frame('columnName')

比如说,

s = pd.Series(["a", "b", "c"], name="vals")
df = s.to_frame('newCol')
print(df)

   newCol
0    a
1    b
2    c

这可能是一种非pythonic的方式,但这将在一行中给出您想要的结果:

new_df = pd.DataFrame(zip(email,list))
结果:

               email               list
0   email1@email.com    [1.0, 0.0, 0.0]
1   email2@email.com    [2.0, 0.0, 0.0]
2   email3@email.com    [1.0, 0.0, 0.0]
3   email4@email.com    [4.0, 0.0, 3.0]
4   email5@email.com    [1.0, 5.0, 0.0]

超级简单的方式也是如此

df = pd.DataFrame(series)

它将返回1列(系列值)+1个索引(0…n)的DF

如果系列具有相同的轴,则另一个伟大的选择是concat([sf.index,sf.values],axis=1)
系列对象到帧()!我输出了这个类类型
为什么要使用
来_frame().重置_index()
而不仅仅是
重置_index
?你甚至可以在熊猫的最新版本中执行
reset\u index(name='list')
。我想知道为什么人们可能会使用
来构建
,而不是
reset\u index
,但是否有充分的理由同时使用这两种方法呢@邓布利多大部分是实用的。如果要使用索引的单列数据帧,请使用to_frame()。如果您需要两列(一列来自系列索引,另一列来自系列值本身),请使用reset_index()。如果我想将系列转换为使用Seires index作为数据帧列名称(即转置)的数据帧,该怎么办
to_frame
似乎没有这样做的参数。谢谢。@confound使用to_frame().T来转置它