根据索引列表组合/求和numy数组的行

时间:2020-05-06 04:56:05

标签: python pandas numpy python-xarray

我有一个数组,我想在其中添加特定的行,以得到行数更少的数组。

import numpy as np
a = np.arange(50).reshape(10,5)
b = [0,0,0,1,1,2,2,2,2,3]
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44],
       [45, 46, 47, 48, 49]])

a.shape
(10, 5)

len(b)
10

我想使用b作为我要合并的行的基础。前三行将被累加成为新的第一行,第四行和第五行将被累加成为新的第二行。依此类推。

所需结果:

array([[15, 18, 21, 24, 27],
       [35, 37, 39, 41, 43],
       [130, 134, 138, 142, 146],
       [45, 46, 47, 48, 49]])

循环对于我的钱包来说是低效的。我不确定这是否可以在numpy中完成,但也许是熊猫还是xarray?

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

在熊猫解决方案中,创建DataFrame并按b数组创建的索引汇总和:

a = np.arange(50).reshape(10, 5)
b = [0,0,0,1,1,2,2,2,2,3]
print (a)

c = pd.DataFrame(a, index=b).sum(level=0).to_numpy()
print (c)
[[ 15  18  21  24  27]
 [ 35  37  39  41  43]
 [130 134 138 142 146]
 [ 45  46  47  48  49]]

答案 1 :(得分:1)

我想添加一个numpy的解决方案:

import numpy as np

a = np.arange(50).reshape(10,5)
b = [0,0,0,1,1,2,2,2,2,3]

sum_common = lambda x : sum(a[b==x,:])

indx = np.unique(b)

c = np.array(map(sum_common, indx))

当然可以一行完成:

c=np.array(map(lambda x : sum(a[b==x,:]), np.unique(b)))

结果:

array([[ 15,  18,  21,  24,  27],
       [ 35,  37,  39,  41,  43],
       [130, 134, 138, 142, 146],
       [ 45,  46,  47,  48,  49]])