在Numpy/Pandas中生成所有平行对角线总和的直接方法? [英] Direct way to generate sum of all parallel diagonals in Numpy / Pandas?

查看:309
本文介绍了在Numpy/Pandas中生成所有平行对角线总和的直接方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩形的Pandas DataFrame(不能假定为正方形).假设我选择了一个对角线方向(从左上到右下"或从右上到左下").我想计算一个序列,其条目是沿着平行对角线的选定集合从原始DataFrame取值的总和.要完全指定目标,您需要确定对角线是在左侧锚定"还是在右侧锚定".对于下面的内容,我认为它们是固定在"左侧的.

I have a rectangular (can't be assumed to be square) Pandas DataFrame of numbers. Say I pick a diagonal direction (either "upperleft to lowerright" or "upperright to lowerleft"). I'd like to compute a series whose entries are the sums of the values from the original DataFrame along the chosen set of parallel diagonals. To fully specify the goal, you need to decide whether diagonals are "anchored" on the left or "anchored" on the right. For the below, I assume they're "anchored" on the left.

我可以做到这一点而没有太多麻烦:

I can do this without too much trouble:

import numpy as np
import pandas as 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 the "upperleft to lowerright" diagonal sums as follows:

ullrsums = pd.concat([rectdf.iloc[:, i].shift(-i) for i in range(rectdf.shape[1])], axis=1)\
    .sum(axis=1, fillna=0)

# result:
0    12
1    21
2    30
3    22
4    12

通过将上一个中的shift(-i)翻转为shift(i),我可以计算出从右上到左下"的对角线总和:

And I can compute the "upperright to lowerleft" diagonal sums by flipping the shift(-i) to shift(i) in the previous:

urllsums = pd.concat([rectdf.iloc[:, i].shift(i) for i in range(rectdf.shape[1])], axis=1)\
    .sum(axis=1, fillna=0)

# result:
0     0
1     4
2    12
3    21
4    30

这些结果都是正确的(即这段代码可以实现我想要的功能).是否有更直接的方法来计算熊猫"或"numpy"中的这些总和?

These results are all correct (i.e. this code does what I want). Is there a more direct way to compute these sums in Pandas or Numpy?

推荐答案

您可能正在寻找numpy.trace(),记录在

You may be looking for numpy.trace(), documented here, to get the trace directly, or numpy.diagonal() to get the diagonal vector, documented here

首先,使用rectdf.as_matrix()

然后:

np.trace(matrix, offset)

偏移量可以是正值也可以是负值,它会执行您需要的移位.

The offset, which can be either positive or negative, does the shifting you require.

例如,如果我们这样做:

For example, if we do:

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

我们得到输出:

12
22
30
21
12
6
2

要对通用矩阵执行此操作,我们希望范围从-(rows - 1)columns,即,如果我们有一个变量rows和一个变量columns:

To do this for a general matrix, we want the range from -(rows - 1) to columns, i.e. if we have a variable rows and a variable columns:

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

这篇关于在Numpy/Pandas中生成所有平行对角线总和的直接方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆