总计行数和总和

时间:2018-11-10 13:03:09

标签: python pandas numpy

我有一个Numpy矩阵:

M = [[55, 5],
 [56, 3],
 [57, 7],
 [58, 9],
 [59, 3],
 [60, 8],
 [61, 1]] 

我想按group_size进行聚合(例如分成3组):

group_size = math.ceil(M.size/groups) # math.ceil(7/3) = 3

每个聚合行的左值是该组中的第一个左值,而右值是该组中所有右值的总和。

预期输出:

R = [[55, 15], # 55 first left column value of first group, 15 sum of all right values in group 
 [58, 20], # 58 first left column value of second group, 20 sum of all right values in group
 [61, 1]] # Third group consist only of one row, remainder

有没有一种有效的方法可以用Numpy解决而不循环?

4 个答案:

答案 0 :(得分:3)

pandas解决方案应与aggfirst一起使用sum

group_size = 3
df = pd.DataFrame(M).groupby(np.arange(len(M)) // group_size).agg({0:'first',1:'sum'})
print (df)
    0   1
0  55  15
1  58  20
2  61   1

a = np.array(df.values.tolist())
print(a)
[[55 15]
 [58 20]
 [61  1]]

答案 1 :(得分:3)

这是NumPy的一种方式:

n = 3
x = M[::n, 0]
y = np.add.reduceat(M[:, 1], np.arange(0, M.shape[0], n))

R = np.vstack((x, y)).T

print(R)

array([[55, 15],
       [58, 20],
       [61,  1]])

答案 2 :(得分:1)

使用Python的解决方案:

from operator import itemgetter

M = [[55, 5],
     [56, 3],
     [57, 7],
     [58, 9],
     [59, 3],
     [60, 8],
     [61, 1]]
it = (M[e:e+3] for e in range(0, len(M), 3))
print([[e[0][0], sum(map(itemgetter(1), e))] for e in it])

输出

[[55, 15], [58, 20], [61, 1]]

答案 3 :(得分:0)

a = np.array([[2, 3],[5, 6],[7, 9]])
b = numpy.zeros(shape=(len(a[0])))
for i in a:
    b=b+i
    print(b)