Jens Modvig
Jens Modvig

Reputation: 183

Inserting A 2D array into another 2D array in python

I want to insert a numpy array A of size 508x12 into another numpy array B of size 508x13 resulting in an array of size 508x25. But here is the thing, i don't just want to concatanete them, but insted insert the array at one specific column location c. enter image description here

How would I do that?, I have tried:

C = np.insert(B, c, A, axis=1)

Upvotes: 0

Views: 899

Answers (1)

jammertheprogrammer
jammertheprogrammer

Reputation: 397

Just split up the concatenation like @brezniczky suggested. Alternatively, use hstack:

import numpy as np


a = np.ones((508,12))
b = np.zeros((508,13))

col = 3

final = np.hstack((b[:,0:col],a,b[:,col:])) 

print(final[0])

[0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

In this scenario hstack and concatenation with axis=1 are the same, I just prefer hstack for better readability

Upvotes: 1

Related Questions