python - Direct way to generate sum of all parallel diagonals in Numpy / Pandas? -


i have rectangular (can't assumed square) pandas dataframe of numbers. pick diagonal direction (either "upperleft lowerright" or "upperright lowerleft"). i'd compute series entries sums of values original dataframe along chosen set of parallel diagonals. specify goal, need decide whether diagonals "anchored" on left or "anchored" on right. below, assume they're "anchored" on left.

i can without trouble:

import numpy np import pandas pd  rectdf = pd.dataframe(np.arange(15).reshape(5,3))  # result:     0   1   2 0   0   1   2 1   3   4   5 2   6   7   8 3   9  10  11 4  12  13  14 

i can compute "upperleft lowerright" diagonal sums follows:

ullrsums = pd.concat([rectdf.iloc[:, i].shift(-i) in range(rectdf.shape[1])], axis=1)\     .sum(axis=1, fillna=0)  # result: 0    12 1    21 2    30 3    22 4    12 

and can compute "upperright lowerleft" diagonal sums flipping shift(-i) shift(i) in previous:

urllsums = pd.concat([rectdf.iloc[:, i].shift(i) in range(rectdf.shape[1])], axis=1)\     .sum(axis=1, fillna=0)  # result: 0     0 1     4 2    12 3    21 4    30 

these results correct (i.e. code want). there more direct way compute these sums in pandas or numpy?

you may looking numpy.trace(), documented here, trace directly, or numpy.diagonal() diagonal vector, documented here

first, convert dataframe numpy matrix using rectdf.as_matrix()

then:

np.trace(matrix, offset) 

the offset, can either positive or negative, shifting require.

for example, if do:

a = np.arange(15).reshape(5, 3) x in range(-4, 3): print np.trace(a, x) 

we output:

12 22 30 21 12 6 2 

to general matrix, want range -(rows - 1) columns, i.e. if have variable rows , variable columns:

a = np.arange(rows * columns).reshape(rows, columns) x in range(-(rows - 1), columns): print np.trace(a, x) 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -