nitin
nitin

Reputation: 7358

Pandas Panel Boolean Selection

0I have pandas Panel from which I need to manipulate based on a decision criteria. Lets say, I have a pandas Panel, like so,

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

I want to manipulate this panel. Manipulation psuedo code is,

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

Now if this were a DataFrame, I would do the following to get my result:

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

But I am not able to implement equivalent login for a Panel.

Any clues would help

Upvotes: 0

Views: 208

Answers (1)

Jeff
Jeff

Reputation: 129018

This requires 0.14.0 (for the non-info axis setting, e.g. 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

You can do a fillna(0) if would like before the assignment.

If I were doing this, I would prob do p.transpose('minor_axis','major_axis','items') first, just to make this code a bit simpler (e.g. p['A'] works then)

Upvotes: 3

Related Questions