Nycolas Mancini
Nycolas Mancini

Reputation: 153

Appending diferent dimention arrays

I have a simlpe question regarding column stacking that I just can't solve on my own. Here is a sample of a array called main and and a 1D array callend values.

print(main)

[[1.04471e+00 1.04548e+00 1.04471e+00 1.04581e+00 0.00000e+00]
[1.04439e+00 1.04624e+00 1.04308e+00 1.04493e+00 1.00000e+00]
[1.04351e+00 1.04450e+00 1.04341e+00 1.04406e+00 2.00000e+00]
...
[6.16370e-01 6.17280e-01 6.11910e-01 6.16560e-01 2.15440e+04]
[6.16280e-01 6.17650e-01 6.14450e-01 6.17780e-01 2.15450e+04]
[6.14770e-01 6.19280e-01 6.06560e-01 6.15110e-01 2.15460e+04]]

print(values)
[       nan        nan        nan ... 0.00534073 0.00416329 0.00886953]

All I want to do is append this 1D array at the end of my main array as a 'column', like

[1.04471e+00 1.04548e+00 1.04471e+00 1.04581e+00 0.00000e+00   nan]
[1.04439e+00 1.04624e+00 1.04308e+00 1.04493e+00 1.00000e+00   nan]
[1.04351e+00 1.04450e+00 1.04341e+00 1.04406e+00 2.00000e+00   nan]
...
[6.16370e-01 6.17280e-01 6.11910e-01 6.16560e-01 2.15440e+04   0.00534073]
[6.16280e-01 6.17650e-01 6.14450e-01 6.17780e-01 2.15450e+04   0.00416329]
[6.14770e-01 6.19280e-01 6.06560e-01 6.15110e-01 2.15460e+04]  0.00886953]

Is it possible?

Upvotes: 0

Views: 22

Answers (1)

Ehsan
Ehsan

Reputation: 12397

You can do it at least two ways.

  1. reshape your values array and then append it to main like this:

    values = values.reshape(-1, 1)
    np.append(main, values, axis=1)
    

    To append to column, use axis=1 property of np.append. And in order for you to be able to append columns, number of rows in main and values need to be the same, so you need to reshape values to have 1 column and -1 will implicitly correct the number of rows.

  2. You can use np.column_stack to do that without reshaping values:

    np.column_stack((main, values))
    

output:

[[1.04471e+00 1.04548e+00 1.04471e+00 1.04581e+00 0.00000e+00         nan]
 [1.04439e+00 1.04624e+00 1.04308e+00 1.04493e+00 1.00000e+00         nan]
 [1.04351e+00 1.04450e+00 1.04341e+00 1.04406e+00 2.00000e+00         nan]
 ...
 [6.16370e-01 6.17280e-01 6.11910e-01 6.16560e-01 2.15440e+04 5.34073e-03]
 [6.16280e-01 6.17650e-01 6.14450e-01 6.17780e-01 2.15450e+04 4.16329e-03]
 [6.14770e-01 6.19280e-01 6.06560e-01 6.15110e-01 2.15460e+04 8.86953e-03]]

Upvotes: 1

Related Questions