dayum
dayum

Reputation: 1093

pandas panel data update

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

Upvotes: 3

Views: 666

Answers (1)

piRSquared
piRSquared

Reputation: 294278

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))

Upvotes: 1

Related Questions