user5779223 user5779223 - 3 months ago 17
Python Question

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

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!

Answer

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.

Comments