Monday, September 30, 2013

Initializing a Pandas panel

Sometimes there are multiple tables of data that should be stored in an aligned manner. Pandas Panel is great for this. Panels can not expand along the major and minor axis after they are created (at least in a painless manner). If you know the maximum size of the tabular data it is convenient to initialize the panel to this maximum size before inserting any data. For example:
import numpy, pandas as pd

pn = pd.Panel(major_axis=['1','2','3','4','5','6'], minor_axis=['a','b'])
pn['A'] = pd.DataFrame(numpy.random.randn(3,2), index=['2','3','5'], columns=['a','b'])
print pn['A']

Which gives:
          a         b
1       NaN       NaN
2  1.862536 -0.966010
3 -0.214348 -0.882993
4       NaN       NaN
5 -1.266505  1.248311
6       NaN       NaN

Edit: Don't need a default item - an empty panel can be created

No comments:

Post a Comment