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 |
+---+-----+-----+-----+
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With