pandas panel data update
<p>I am using panel datastructure in pandas for storing a 3d panel. </p>

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

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

<pre><code> u=T.get_value(n1_loop,n2_loop,n3_loop)
 T.set_value(n1_loop,n2_loop,n3_loop,u+1)
</code></pre>

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

<pre><code>T[n1_loop,n2_loop,n3_loop] +=1
</code></pre>

<p>or </p>

<pre><code>T[n1_loop,n2_loop,n3_loop] = T[n1_loop,n2_loop,n3_loop] +1
</code></pre>

<p><strong><em>TL;DR</em></strong>  </p>

<pre><code>T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))
</code></pre>

<hr>

<p>The analogous exercise for a DataFrame would be to assign with <code>loc</code></p>

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

<p><a href="https://i.stack.imgur.com/qIupI.png" rel="nofollow"><img src="https://i.stack.imgur.com/qIupI.png" alt="enter image description here"></a></p>

<hr>

<p>Exact <code>pd.Panel</code> analog generates an Error</p>

<pre><code>pn = pd.Panel(np.zeros((5, 5, 2)), list('abcde'), list('ABCDE'), list('XY'), int)
pn.loc['b':'d', list('AE'), ['X']] += 1
pn
</code></pre>

<blockquote>
<pre><code>NotImplementedError: cannot set using an indexer with a Panel yet!
</code></pre>
</blockquote>

<hr>

<p>But we can still slice it</p>

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

&lt;class 'pandas.core.panel.Panel'&gt;
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
</code></pre>

<hr>

<p>And we can use the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.update.html" rel="nofollow"><code>update</code> method</a></p>

<pre><code>pn.update(pn.loc['b':'d', list('AE'), ['X']].add(1))
</code></pre>

<p>Which we can see did stuff if we use the <code>to_frame</code> method</p>

<pre><code>pn.to_frame().astype(int)
</code></pre>

<p><a href="https://i.stack.imgur.com/59N5f.png" rel="nofollow"><img src="https://i.stack.imgur.com/59N5f.png" alt="enter image description here"></a></p>

<p><strong><em>OR</em></strong></p>

<pre><code>pn.loc[:, :, 'X'].astype(int).T
</code></pre>

<p><a href="https://i.stack.imgur.com/qIupI.png" rel="nofollow"><img src="https://i.stack.imgur.com/qIupI.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>YOUR CASE</em></strong><br>
This should work</p>

<pre><code>T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))
</code></pre>

python panel-data pandas