Home > Blockchain >  Merge arrays on non-nan values
Merge arrays on non-nan values

Time:10-01

In numpy, how would you merge the following two arrays on the non-nan values, resulting in third array?

Array 1 (shape: r, c):

array([[nan,  1.,  1., nan, nan, nan],
       [nan, nan, nan,  1.,  1., nan],
       [nan, nan,  1.,  1., nan, nan],
       ...,
       [nan, nan, nan,  1.,  1., nan],
       [nan, nan, nan,  1.,  1., nan],
       [nan, nan,  1.,  1., nan, nan]])

Array 2: (shape r, 2)

array([[ 0.76620125, 59.14934823],
       [ 2.52819832, 43.63809538],
       [ 1.9656387 , 25.62212163],
       ...,
       [ 2.55076928, 43.04276273],
       [ 2.62058763, 22.14260189],
       [ 1.8050997 , 51.72144285]])

Resulting array: (shape r, c)

array([[nan,  0.76620125, 59.14934823, nan, nan, nan],
       [nan, nan, nan,   2.52819832, 43.63809538, nan],
       [nan, nan,   1.9656387, 25.62212163, nan, nan],
       ...,
       [nan, nan, nan,   2.55076928, 43.04276273, nan],
       [nan, nan, nan,   2.62058763, 22.14260189, nan],
       [nan, nan,   1.8050997 , 51.72144285, nan, nan]])

CodePudding user response:

this should do, if a is your first array and b the second one:

a[~np.isnan(a)] = b.ravel()
  • Related