Reputation: 28659
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 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 |
+---+-----+-----+-----+
Upvotes: 3
Views: 741
Reputation: 294218
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
Upvotes: 1