pandas 如何将多维的熊猫系列转换为熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49392915/
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-09-14 05:21:13  来源:igfitidea点击:

How do I convert a pandas series which is multidimensional to pandas dataframe

pythonpandasnumpydataframe

提问by Greeshma Agasthya

I am fairly new to python. Presently, I have a pandas series pds

我对python相当陌生。目前,我有一个Pandas系列pds

pds.shape
#(1159,)

pdshas an index which is not sequential and at each index there is a (18,100)array

pds有一个不连续的索引,并且在每个索引处都有一个(18,100)数组

pds[pds.index[1]].shape
#(18, 100)

How can I convert this to a pandas dataframe and/or a numpy array with dimensions (1159,18,100)?

如何将其转换为 Pandas 数据框和/或具有维度的 numpy 数组(1159,18,100)

pdf = pd.DataFrame(pds)

gives me a pandas with shape

给我一只有形状的Pandas

pdf.shape
(1159, 1)

采纳答案by Pavel Komarov

Does this work: numpy.stack([pds[pds.index[i]] for i in range(1159)], axis=0)?

这是否有效:numpy.stack([pds[pds.index[i]] for i in range(1159)], axis=0)

stackshould put all your arrays together along the axis given.

stack应该沿着给定的轴将所有数组放在一起。

回答by ChootsMagoots

You say you want to keep indexing, that means numpy is out (someone already posted a numpy solution as well). My recommendation would be to create a series of DataFrames, as panels are deprecated.

你说你想保持索引,这意味着 numpy 已经过时了(有人已经发布了一个 numpy 解决方案)。我的建议是创建一系列 DataFrame,因为面板已被弃用。

new series = pd.Series()
for index, element in pds: 
    new_series.append(pd.DataFrame(element))

Should do the trick.

应该做的伎俩。