将Pandas Series转换为DataFrame

woshitom:

我有一个熊猫系列科幻小说:

email
[email protected]    [1.0, 0.0, 0.0]
[email protected]    [2.0, 0.0, 0.0]
[email protected]    [1.0, 0.0, 0.0]
[email protected]    [4.0, 0.0, 0.0]
[email protected]    [1.0, 0.0, 3.0]
[email protected]    [1.0, 5.0, 0.0]

我想将其转换为以下DataFrame:

index | email             | list
_____________________________________________
0     | [email protected]  | [1.0, 0.0, 0.0]
1     | [email protected]  | [2.0, 0.0, 0.0]
2     | [email protected]  | [1.0, 0.0, 0.0]
3     | [email protected]  | [4.0, 0.0, 0.0]
4     | [email protected]  | [1.0, 0.0, 3.0]
5     | [email protected]  | [1.0, 5.0, 0.0]

我找到了一种方法,但是我怀疑这是更有效的方法:

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)
EdChum:

无需创建2个临时df,您可以使用DataFrame构造函数将它们作为参数传递给dict中:

pd.DataFrame({'email':sf.index, 'list':sf.values})

有很多方法可以构建df,请参阅文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章