Pandas面板布尔选择

时间:2014-07-18 19:33:56

标签: python pandas

0我有pandas Panel,我需要根据决策标准进行操作。可以说,我有一个像这样的熊猫面板,

import pandas.util.testing as tm
pnl = tm.makePanel(3)
print (pnl)
print (pnl.ItemA)


<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D
                   A         B         C         D
2000-01-03 -4.124501  0.544448  0.137366  0.546547
2000-01-04  1.164209  1.913564  2.425440  0.246716
2000-01-05  0.205536  0.029114  0.593224 -1.485217

我想操纵这个面板。操纵伪代码是,

if panel (minor axis) A > panel (minor axis) B: 
    create panel (minor axis) E = panel (minor axis )A
else:
    create panel (minor axis) E = 0

现在,如果这是一个DataFrame,我会执行以下操作来获取结果:

df = tm.makeDataFrame()
j = df.index[df.A>df.B]
df['E']= df.A[j]
df.E.fillna(0, inplace = True)
print (df[:5])
                   A         B         C         D         E
7r7NQtGLeu  0.841597  0.086931 -0.047042  0.047391  0.841597
UX8yXWSk9A -0.227253  0.916731 -1.019982 -0.846820  0.000000
frYd43R5Vh  0.791719  0.611619  0.823532 -0.635672  0.791719
SGOz5FPu5D  0.023616 -0.218383 -0.469084  0.647841  0.023616
mgeS7Nz2yY -1.557350 -1.333223 -0.565624 -1.341025  0.000000

但我无法为Panel实现等效登录。

任何线索都会有所帮助

1 个答案:

答案 0 :(得分:3)

这需要0.14.0(对于非信息轴设置,例如p.loc[:,:,'E'] = ....

In [9]: p
Out[9]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D

In [10]: p.loc[:,:,'A']
Out[10]: 
               ItemA     ItemB     ItemC
2000-01-03  0.712815  1.220183 -0.291173
2000-01-04  1.409244  1.766220 -1.438192
2000-01-05  1.394279 -0.006321  1.500179

In [13]: p.loc[:,:,'B']
Out[13]: 
               ItemA     ItemB     ItemC
2000-01-03 -1.487971  0.694603 -0.013374
2000-01-04 -0.932481 -0.836497  0.098289
2000-01-05  1.239921 -0.330534  0.582391

In [11]: p.loc[:,:,'A']>p.loc[:,:,'B']
Out[11]: 
           ItemA ItemB  ItemC
2000-01-03  True  True  False
2000-01-04  True  True  False
2000-01-05  True  True   True

In [12]: p.loc[:,:,'A'].where(p.loc[:,:,'A']>p.loc[:,:,'B'])
Out[12]: 
               ItemA     ItemB     ItemC
2000-01-03  0.712815  1.220183       NaN
2000-01-04  1.409244  1.766220       NaN
2000-01-05  1.394279 -0.006321  1.500179

In [14]: p.loc[:,:,'E'] = p.loc[:,:,'A'].where(p.loc[:,:,'A']>p.loc[:,:,'B'])

In [15]: p
Out[15]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 3 (major_axis) x 5 (minor_axis)
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to E

In [16]: p.loc[:,:,'E']
Out[16]: 
               ItemA     ItemB     ItemC
2000-01-03  0.712815  1.220183       NaN
2000-01-04  1.409244  1.766220       NaN
2000-01-05  1.394279 -0.006321  1.500179

如果在转让之前愿意,可以fillna(0)

如果我这样做,我会先做p.transpose('minor_axis','major_axis','items'),只是为了让这段代码更简单(例如p['A']就可以了)