piRSquared piRSquared - 2 months ago 7
Python Question

find the manhatten sum of a dataframe given two points

consider the dataframe

df


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


enter image description here

and two points
p0
and
p1


p0 = ('a', 'B')
p1 = ('d', 'E')


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

Assumptions


  • p0
    is always to the left or same column as
    p1

  • p0
    is always above or same row as
    p1

  • path should go down first then right from
    p0
    to
    p1



enter image description here

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'