Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将数据帧列表转换为面板?_Python_Pandas_Dataframe_Panel - Fatal编程技术网

如何在python中将数据帧列表转换为面板?

如何在python中将数据帧列表转换为面板?,python,pandas,dataframe,panel,Python,Pandas,Dataframe,Panel,给定具有以下格式的数据帧列表: id age weight score date 01 11 50 90 2011-01-23 01 12 52 89 2012-03-23 ... 请注意,数据框中的id是相同的。我希望得到一个面板,集成列表中的所有数据帧,列['age'、'weight'、'score']为项目轴,列日期为长轴,列id为短轴。你知道怎么做吗 提前谢谢你 第一步是concat concated = pd.concat(

给定具有以下格式的数据帧列表:

id  age  weight  score  date 
01  11   50      90     2011-01-23
01  12   52      89     2012-03-23
...
请注意,数据框中的
id
是相同的。我希望得到一个面板,集成列表中的所有数据帧,列
['age'、'weight'、'score']
项目轴
,列
日期
长轴
,列
id
短轴
。你知道怎么做吗


提前谢谢你

第一步是
concat

 concated = pd.concat(list_of_frames)
然后,您可以简单地:

items = ['age', 'weight', 'score']
pd.Panel(dict(zip(items, [concated.pivot(index='date', columns='id', values=i) for i in items])))

这篇文章很好地说明了这一点。

到目前为止,你都做了些什么?@Kartik我被困在索引中了。根据这一点,我应该构建一个多索引。但是我不知道如何正确地构建它。哦,这是一种复杂的方式。看看我的答案。谢谢你的回答!你的意思是
pd.Panel(dict(项目[df.pivot(index='date',columns='id',value=[i for i in items]))
?但我得到了一个错误:
异常:数据必须是一维的。你知道吗?谢谢我的意思是用
dict
,是的。我忘记了口述,但当我意识到这一点时,我进行了编辑。它与
dict
一起工作吗?您能检查一下代码中的括号吗?因为我得到了
SyntaxError:invalid syntaxe
而严格复制代码是的,
values=I
后面有一个结束括号,我错过了。谢谢你现在能检查一下吗?我已经解决了这个问题。你的解决方案真的很有效!非常感谢!