添加2D numpy数组1D明智/元素明智

时间:2019-09-10 13:21:12

标签: python arrays numpy numpy-ndarray

假设我有一个形状为4x5的ndarray

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [ 10, 11, 12, 13, 14],
       [ 15, 16, 17, 18, 19]])

是否有任何内置的numpy函数来按元素逐行添加并获得以下结果:

array([30, 34, 38, 42, 46])

或采用任何程序在固定时间内添加这些列?

1 个答案:

答案 0 :(得分:0)

您也可以通过pandas进行此操作:

In [64]: import numpy as np
In [65]: import pandas as pd
In [66]: a = np.array([[ 0,  1,  2,  3,  4],
    ...:        [ 5,  6,  7,  8,  9],
    ...:        [ 10, 11, 12, 13, 14],
    ...:        [ 15, 16, 17, 18, 19]])

In [72]: pd.DataFrame(a).sum().to_numpy()
Out[72]: array([30, 34, 38, 42, 46])