Extending a pandas panel frame along the minor axis

自古美人都是妖i 提交于 2019-11-30 04:07:32

This doesn't work right now see this, https://github.com/pydata/pandas/issues/2578 But you can accomplish what you want this way. This is a pretty cheap operation as nothing is copied.

In [18]: x = pf.transpose(2,0,1)

In [19]: x
Out[19]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 100 (minor_axis)
Items axis: A to D
Major_axis axis: df1 to df3
Minor_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00

In [20]: x['E'] = new_df

In [21]: x.transpose(1,2,0)
Out[21]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 100 (major_axis) x 5 (minor_axis)
Items axis: df1 to df3
Major_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00
Minor_axis axis: A to E

It seems like the bug was fixed but your question interested me.

Since you can effectively add a slice to a panel on the major and minor axis without transposing, the following 2 lines can avoid scratching your head on the size of the Dataframe...

pf.ix[:,'another major axis',:] = pd.DataFrame(np.random.randn(pf.minor_axis.shape[0],pf.items.shape[0]), index=pf.minor_axis, columns=pf.items)

pf.ix[:, :, 'another minor axis'] = pd.DataFrame(np.random.randn(pf.major_axis.shape[0],pf.items.shape[0]), index=pf.major_axis, columns=pf.items)

I wondered however if there was something simpler ?

Below the piece of code that add slices along various axes.

import pandas as pd
import numpy as np

rng = pd.date_range('25/11/2014', periods=2, freq='D')
df1 = pd.DataFrame(np.random.randn(2, 5), index=rng, columns=['A', 'B', 'C', 'D', 'E'])
df2 = pd.DataFrame(np.random.randn(2, 5), index=rng, columns=['A', 'B', 'C', 'D', 'E'])
df3 = pd.DataFrame(np.random.randn(2, 5), index=rng, columns=['A', 'B', 'C', 'D', 'E'])


pf = pd.Panel({'df1': df1, 'df2': df2, 'df3': df3})

# print("slice before adding df4:\n")
# for i in pf.items:
#     print("{}:\n{}".format(i, pf[i]))

pf['df4'] = pd.DataFrame(np.random.randn(pf.major_axis.shape[0], pf.minor_axis.shape[0]), index=pf.major_axis, columns=pf.minor_axis)
print pf

# print("slice after df4 before transposing 1:\n")
# for i in pf.items:
#     print("{}:\n{}".format(i, pf[i]))

x = pf.transpose(1, 0, 2)

x['new major axis item'] = pd.DataFrame(np.random.randn(pf.items.shape[0], pf.minor_axis.shape[0]), index=pf.items,
                                        columns=pf.minor_axis)

pf = x.transpose(1, 0, 2)

print pf
# print("slice after:\n")
# for i in pf.items:
#     print("{}:\n{}".format(i, pf[i]))

print("success on adding slice on major axis:")
print pf.major_xs(key='new major axis item')
print("trying to add major axis directly")
pf.ix[:,'another major axis',:] = pd.DataFrame(np.random.randn(pf.minor_axis.shape[0],pf.items.shape[0]), index=pf.minor_axis, columns=pf.items)

print pf.major_xs(key='another major axis')
print pf
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!