Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform pandas panel into dataframe

Tags:

python

pandas

I have a pandas.Panel, and I want to create a pandas.DataFrame with the column headers coming from one column, the data from another column, and the number of rows is the number of items in the panel.

If diagrams will help describe what I'm looking for...

My panel looks somewhat like the following:

      +---+---------+------------+------+
    +---+---------+------------+------+ |
  +---+---------+------------+------+ |-+
+---+---------+------------+------+ |-+ |
|   | context | iterations | time |-+ |-+
+---+---------+------------+------+ |-+ |
| 0 | foo     |          1 |   21 |-+ |-+
+---+---------+------------+------+ |-+ |
| 1 | bar     |          2 |   37 |-+ |-+
+---+---------+------------+------+ |-+
| 2 | baz     |          1 |   53 |-+
+---+---------+------------+------+

I would like to transform the panel into a dataframe:

  • The dataframe column headers are the "context" column
  • The dataframe values are the "time" column
  • The number of rows in the dataframe is the number of items in the panel

The result would look something like this:

+---+-----+-----+-----+
|   | foo | bar | baz |
+---+-----+-----+-----+
| 0 |  21 |  37 |  53 |
+---+-----+-----+-----+
| 1 |  36 |  42 |  76 |
+---+-----+-----+-----+
| 2 |  24 |  56 |  83 |
+---+-----+-----+-----+
| 3 |  17 |  32 |  45 |
+---+-----+-----+-----+
like image 633
Steve Lorimer Avatar asked Apr 24 '26 21:04

Steve Lorimer


1 Answers

Option 1
pd.concat

pd.concat({i: d.set_index('context').time for i, d in pn.iteritems()}).unstack()

context  foo  bar  baz
0         21   37   53
1         36   42   76
2         24   56   83
3         17   32   45

Option 2
pd.DataFrame

pd.DataFrame([d.set_index('context').time for i, d in pn.iteritems()], pn.items)

context  foo  bar  baz
0         21   37   53
1         36   42   76
2         24   56   83
3         17   32   45
like image 174
piRSquared Avatar answered Apr 26 '26 09:04

piRSquared