I have a pandas panel full of data frames. I'd like to concatenate the data frames in the panel. pandas.concat takes a list or dictionary of data frames but not a panel. Is there an easy way to do this? I suppose if I convert the Panel to a dictionary then I can concatenate it.
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"