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
How do I convert a pandas series which is multidimensional to pandas dataframe
提问by Greeshma Agasthya
I am fairly new to python. Presently, I have a pandas series pds
我对python相当陌生。目前,我有一个Pandas系列pds
pds.shape
#(1159,)
pds
has 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
回答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.
应该做的伎俩。