piRSquared - 2 months ago 7

Python Question

consider the dataframe

`df`

`df = pd.DataFrame(np.arange(25).reshape(5, 5), list('abcde'), list('ABCDE'))`

df

and two points

`p0`

`p1`

`p0 = ('a', 'B')`

p1 = ('d', 'E')

I want to find the sum of all elements along the path from point

`p0`

`p1`

Assumptions

- is always to the left or same column as
`p0`

`p1`

- is always above or same row as
`p0`

`p1`

- path should go down first then right from to
`p0`

`p1`

I'm expecting a value of

`88`

Answer

I *think* 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:

```
s1 = df.ix['a':'d', 'B'].sum()
s2 = df.ix['d', 'B':'E'].sum()
print s1 + s2 - df.ix['d', 'B']
```

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:

`p0[0]`

for`'a'`

`p1[0]`

for`'d'`

`p0[1]`

for`'B'`

`p1[1]`

for`'E'`