3

I am using panel datastructure in pandas for storing a 3d panel.

T=pd.Panel(data=np.zeros((n1,n2,n3)),items=n1_label, major_axis=n2_label, minor_axis=n3_label)

Later on, I am trying to update (increment) the values stored at individual locations inside a loop. Currently I am doing:

 u=T.get_value(n1_loop,n2_loop,n3_loop)
 T.set_value(n1_loop,n2_loop,n3_loop,u+1)

My question- is this the simplest way? Is there any other simpler way? The following dont work:

T[n1_loop,n2_loop,n3_loop] +=1

or

T[n1_loop,n2_loop,n3_loop] = T[n1_loop,n2_loop,n3_loop] +1
dayum
  • 1,073
  • 15
  • 31

1 Answers1

1

TL;DR

T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))

The analogous exercise for a DataFrame would be to assign with loc

df = pd.DataFrame(np.zeros((5, 5)), list('abcde'), list('ABCDE'), int)
df.loc['b':'d', list('AE')] += 1
df

enter image description here


Exact pd.Panel analog generates an Error

pn = pd.Panel(np.zeros((5, 5, 2)), list('abcde'), list('ABCDE'), list('XY'), int)
pn.loc['b':'d', list('AE'), ['X']] += 1
pn
NotImplementedError: cannot set using an indexer with a Panel yet!

But we can still slice it

pn = pd.Panel(np.zeros((5, 5, 2)), list('abcde'), list('ABCDE'), list('XY'), int)
pn.loc['b':'d', list('AE'), ['X']]

<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 2 (major_axis) x 1 (minor_axis)
Items axis: b to d
Major_axis axis: A to E
Minor_axis axis: X to X

And we can use the update method

pn.update(pn.loc['b':'d', list('AE'), ['X']].add(1))

Which we can see did stuff if we use the to_frame method

pn.to_frame().astype(int)

enter image description here

OR

pn.loc[:, :, 'X'].astype(int).T

enter image description here


YOUR CASE
This should work

T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • somehow i get this error on trying T.update(T.loc[n1_loop, n2_loop, n3_loop].add(1)) AttributeError: 'numpy.float64' object has no attribute 'add' – dayum Oct 13 '16 at 01:28
  • @dayum I assumed your loop variables were lists. – piRSquared Oct 13 '16 at 02:04
  • sorry probably should have mentioned this earlier, but n1_loop, n2_loop and n3_loop are all strings (referencing the indices along 3 axis) – dayum Oct 13 '16 at 04:34
  • @dayum if `n1_loop` is a string that is a single member of the `items` index and so on, then use `T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))` – piRSquared Oct 13 '16 at 04:43