沿次轴扩展pandas面板框架

3 投票
2 回答
7537 浏览
提问于 2025-04-17 18:48

我想在pandas中扩展一个数据面板的次轴。首先,我创建了一个包含多个DataFramedic,用来生成一个面板。

import pandas as pd
import numpy as np
rng = pd.date_range('1/1/2013',periods=100,freq='D')
df1 = pd.DataFrame(np.random.randn(100, 4), index = rng, columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.random.randn(100, 4), index = rng, columns = ['A','B','C','D'])
df3 = pd.DataFrame(np.random.randn(100, 4), index = rng, columns = ['A','B','C','D'])
pf = pd.Panel({'df1':df1,'df2':df2,'df3':df3})

如我所预期,我得到了一个面板,尺寸如下:

尺寸:3(项目)x 100(主轴)x 4(次轴) 项目轴: df1 到 df3 主轴:2013-01-01 00:00:00 到 2013-04-10 00:00:00 次轴:A 到 D

现在我想在次轴上添加一个新的数据集:

pf['df1']['E'] = pd.DataFrame(np.random.randn(100, 1), index = rng)
pf['df2']['E'] = pd.DataFrame(np.random.randn(100, 1), index = rng)
pf['df2']['E'] = pd.DataFrame(np.random.randn(100, 1), index = rng)

我发现,在添加这个新的次轴后,面板的数组维度形状并没有改变:

shape(pf)

[3,100,4]

我可以访问主轴中每个项目的数据:

pf.ix['df1',-10:,'E']

2013-04-01 0.168205 2013-04-02 0.677929 2013-04-03 0.845444 2013-04-04 0.431610 2013-04-05 0.501003 2013-04-06 -0.403605 2013-04-07 -0.185033 2013-04-08 0.270093 2013-04-09 1.569180 2013-04-10 -1.374779 频率:D,名称:E

但是如果我扩展切片,包含多个主轴时:

pf.ix[:,:,'E']

我就会遇到一个错误,提示'E'是未知的。

有没有人能建议我哪里出错了,或者有什么更好的方法来执行这个操作?

2 个回答

3

看起来这个问题已经解决了,但你的提问让我很感兴趣。

因为你可以在主要和次要轴上有效地添加切片,而不需要转置,所以下面这两行代码可以让你不用为数据框的大小而烦恼……

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)

不过我在想,是否有更简单的方法呢?

下面是那段可以沿着不同轴添加切片的代码。

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
5

现在这个方法不太好用,可以看看这个链接,https://github.com/pydata/pandas/issues/2578。不过你可以用另一种方式来实现你想要的效果。这种方法很简单,因为没有什么东西被复制。

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

撰写回答