How to convert a list of data frames to a panel in python-pandas?

坚强是说给别人听的谎言 提交于 2019-12-11 04:48:59

问题


Given a list of data frames with the following format:

id  age  weight  score  date 
01  11   50      90     2011-01-23
01  12   52      89     2012-03-23
...

Please note that the id in a data frame is same. And I wish to get a panel, integrating all the data frames in the list, and with the columns ['age', 'weight', 'score'] as item-axis, and date as major-axis, and id as minor-axis. Do you know how to do that?

Thank you in advance!


回答1:


First step is to concat your frames together:

 concated = pd.concat(list_of_frames)

Then, you can simply:

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

This is so nicely specified in this documentation.



来源:https://stackoverflow.com/questions/38886698/how-to-convert-a-list-of-data-frames-to-a-panel-in-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!