Convert pandas panel to dataframe

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

pandas.DataFrame

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

My panel looks something like this:

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

      

I would like to convert a panel to a dataframe:

  • The dataframe column headers are "context" column
  • The dataframe value is the "time <
  • The number of rows in a data frame is the number of items in the panel

The result will look something like this:

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

      

+3


source to share


1 answer


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

      

+1


source







All Articles