Python 将 Pandas 系列转换为 DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26097916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:04:55  来源:igfitidea点击:

Convert pandas Series to DataFrame

pythonpandasdataframeseries

提问by woshitom

I have a Pandas series sf:

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

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]

And I would like to transform it to the following DataFrame:

我想将其转换为以下 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]

I found a way to do it, but I doubt it's the more efficient one:

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

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)

采纳答案by EdChum

Rather than create 2 temporary dfs you can just pass these as params within a dict using the DataFrame constructor:

您可以使用 DataFrame 构造函数将它们作为 dict 中的参数传递,而不是创建 2 个临时 dfs:

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

There are lots of ways to construct a df, see the docs

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

回答by Shoresh

to_frame():

to_frame():

Starting with the following Series, df:

从以下系列开始,df:

email
[email protected]    A
[email protected]    B
[email protected]    C
dtype: int64

I use to_frameto convert the series to DataFrame:

我使用to_frame将系列转换为 DataFrame:

df = df.to_frame().reset_index()

    email               0
0   [email protected]    A
1   [email protected]    B
2   [email protected]    C
3   [email protected]    D

Now all you need is to rename the column name and name the index column:

现在您只需要重命名列名并命名索引列:

df = df.rename(columns= {0: 'list'})
df.index.name = 'index'

Your DataFrame is ready for further analysis.

您的 DataFrame 已准备好进行进一步分析。

Update: I just came across this linkwhere the answers are surprisingly similar to mine here.

更新:我刚刚看到这个链接,这里的答案与我的惊人相似。

回答by Mysterious

One line answer would be

一行答案是

myseries.to_frame(name='my_column_name')
myseries.reset_index(drop=True, inplace=True)  # As needed

回答by cs95

Series.reset_indexwith nameargument

Series.reset_indexname论据

Often the use case comes up where a Series needs to be promoted to a DataFrame. But if the Series has no name, then reset_indexwill result in something like,

通常会出现需要将 Series 提升为 DataFrame 的用例。但是如果系列没有名字,那么reset_index会导致类似的结果,

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

A
a    1
b    2
c    3
dtype: int64

s.reset_index()

   A  0
0  a  1
1  b  2
2  c  3

Where you see the column name is "0". We can fix this be specifying a nameparameter.

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

s.reset_index(name='B')

   A  B
0  a  1
1  b  2
2  c  3

s.reset_index(name='list')

   A  list
0  a     1
1  b     2
2  c     3


Series.to_frame

Series.to_frame

If you want to create a DataFrame without promoting the index to a column, use Series.to_frame, as suggested in this answer. This alsosupports a name parameter.

如果您想在不将索引提升到列的情况下创建 DataFrame,请使用Series.to_frame,如本答案中建议的那样。这支持名称参数。

s.to_frame(name='B')

   B
A   
a  1
b  2
c  3


pd.DataFrameConstructor

pd.DataFrame构造函数

You can also do the same thing as Series.to_frameby specifying a columnsparam:

你也可以Series.to_frame通过指定columns参数来做同样的事情:

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

   B
A   
a  1
b  2
c  3

回答by Giorgos Myrianthous

Series.to_framecan be used to convert a Seriesto DataFrame.

Series.to_frame可用于将 a 转换SeriesDataFrame.

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


For example,

例如,

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

   newCol
0    a
1    b
2    c