find the manhatten sum of a dataframe given two points
<p>consider the dataframe <code>df</code></p>

<pre><code>df = pd.DataFrame(np.arange(25).reshape(5, 5), list('abcde'), list('ABCDE'))
df
</code></pre>

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

<p>and two points <code>p0</code> and <code>p1</code></p>

<pre><code>p0 = ('a', 'B')
p1 = ('d', 'E')
</code></pre>

<p>I want to find the sum of all elements along the path from point <code>p0</code> to <code>p1</code>.<br>
Assumptions</p>

<ul>
<li><code>p0</code> is always to the left or same column as <code>p1</code></li>
<li><code>p0</code> is always above or same row as <code>p1</code></li>
<li>path should go down first then right from <code>p0</code> to <code>p1</code></li>
</ul>

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

<p>I'm expecting a value of <code>88</code></p>

<p>I <em>think</em> you can just sum down the first column and across the row.  This will double count the bottom left item so you need to remove that one:</p>

<pre><code>s1 = df.ix['a':'d', 'B'].sum()
s2 = df.ix['d', 'B':'E'].sum()
print s1 + s2 - df.ix['d', 'B']
</code></pre>

<p>Or, if you prefer, you can do:</p>

<pre><code>s2 = df.ix['d', 'B':'E'][1:].sum()
print s1 + s2
</code></pre>

<p>As this will slice off the element that would have been double counted...</p>

<p>Perhaps a pandas guru can come up with a more efficient or more clever way to do it -- But this seems to work OK.  I've cheated a little bit and hard-coded the points, but it should be easy enough to unravel that -- Just substitute:</p>

<ul>
<li><code>p0[0]</code> for <code>'a'</code></li>
<li><code>p1[0]</code> for <code>'d'</code></li>
<li><code>p0[1]</code> for <code>'B'</code></li>
<li><code>p1[1]</code> for <code>'E'</code></li>
</ul>

python pandas numpy dataframe