连接Pandas面板中所有DataFrame的最简单方法是什么?

时间:2014-03-25 23:54:31

标签: python pandas concatenation dataframe

我有一个充满数据框架的pandas面板。我想连接面板中的数据框。 pandas.concat获取数据框的列表或字典,但不是面板。是否有捷径可寻?我想如果我将Panel转换为字典,那么我可以连接它。

Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pandas

In [2]: data_frames = {}

In [3]: import numpy as np

In [5]: for i in range(3):
    data_frames[i] = pandas.DataFrame(np.random.random((4, 4)))
   ...:     

In [6]: p = pandas.Panel(data_frames)

In [7]: p
Out[7]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 4 (major_axis) x 4 (minor_axis)
Items axis: 0 to 2
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 3

In [10]: c = pandas.concat(data_frames, ignore_index=True)

In [11]: c
Out[11]: 
           0         1         2         3
0   0.181703  0.375656  0.264426  0.627724
1   0.098423  0.197307  0.032177  0.780775
2   0.854787  0.338352  0.769010  0.084029
3   0.153036  0.563780  0.164906  0.588682
4   0.995582  0.757451  0.304299  0.838461
5   0.728513  0.080770  0.817670  0.515151
6   0.020350  0.902724  0.316773  0.495761
7   0.241298  0.337299  0.534545  0.339601
8   0.080109  0.657868  0.786786  0.234976
9   0.684836  0.180652  0.708958  0.132093
10  0.962909  0.525090  0.333035  0.482024
11  0.117683  0.756001  0.037237  0.463168

[12 rows x 4 columns]

In [13]: p[0]
Out[13]: 
          0         1         2         3
0  0.181703  0.375656  0.264426  0.627724
1  0.098423  0.197307  0.032177  0.780775
2  0.854787  0.338352  0.769010  0.084029
3  0.153036  0.563780  0.164906  0.588682

[4 rows x 4 columns]

In [14]: c = pandas.concat(p, ignore_index=True)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-14-d22562d31e50> in <module>()
----> 1 c = pandas.concat(p, ignore_index=True)

/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.pyc in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity)
    927                        ignore_index=ignore_index, join=join,
    928                        keys=keys, levels=levels, names=names,
--> 929                        verify_integrity=verify_integrity)
    930     return op.get_result()
    931 

/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.pyc in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity)
    942             raise AssertionError('first argument must be a list-like of pandas '
    943                                  'objects, you passed an object of type '
--> 944                                  '"{0}"'.format(type(objs).__name__))
    945 
    946         if join == 'outer':

AssertionError: first argument must be a list-like of pandas objects, you passed an object of type "Panel"

1 个答案:

答案 0 :(得分:0)

使用to_frame()方法。要获得所需的输出,您需要转置轴并在之后重置索引。

In [31]: p.transpose(2, 0, 1).to_frame().reset_index(drop=True)
Out[31]: 
           0         1         2         3
0   0.780538  0.412587  0.587926  0.217103
1   0.019309  0.246170  0.965017  0.839562
2   0.742044  0.829160  0.456377  0.250517
3   0.206528  0.753775  0.533168  0.832652
4   0.913328  0.319370  0.236834  0.060967
5   0.684704  0.804084  0.321675  0.792670
6   0.717193  0.720800  0.243751  0.285807
7   0.074519  0.478942  0.327018  0.027021
8   0.406145  0.667695  0.136186  0.434807
9   0.137058  0.964003  0.082268  0.425817
10  0.067491  0.708419  0.352805  0.522568
11  0.738875  0.101868  0.062147  0.085819

[12 rows x 4 columns]
相关问题