我有一个 pandas.Panel
,我想创建一个 pandas.DataFrame
,列标题来自一列,另一列来自另一列列,行数是面板中的项目数。
如果图表有助于描述我正在寻找的内容......
我的面板看起来有点像:
+---+---------+------------+------+
+---+---------+------------+------+ |
+---+---------+------------+------+ |-+
+---+---------+------------+------+ |-+ |
| | context | iterations | time |-+ |-+
+---+---------+------------+------+ |-+ |
| 0 | foo | 1 | 21 |-+ |-+
+---+---------+------------+------+ |-+ |
| 1 | bar | 2 | 37 |-+ |-+
+---+---------+------------+------+ |-+
| 2 | baz | 1 | 53 |-+
+---+---------+------------+------+
我想将面板转换为数据框:
结果看起来像这样:
+---+-----+-----+-----+
| | foo | bar | baz |
+---+-----+-----+-----+
| 0 | 21 | 37 | 53 |
+---+-----+-----+-----+
| 1 | 36 | 42 | 76 |
+---+-----+-----+-----+
| 2 | 24 | 56 | 83 |
+---+-----+-----+-----+
| 3 | 17 | 32 | 45 |
+---+-----+-----+-----+
答案 0 :(得分:1)
选项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
选项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