How to concatenate NumPy array to another array

Numpy

In this post we are going to learn how to concatenate NumPy array to another array by using built in Numpy function np.concatenate() ,np.append(),np.stack(),np.hstack(), column_stack().

How to concatenate NumPy array to another array


  • Concatenate NumPy array to another array .
  • np.append() to concatenate NumPy array to another array .
  • np.vstack() to concatenate NumPy array to another array .
  • np.hstack() to concatenate NumPy array to another array .
  • colum_stack() to concatenate NumPy array to another array .

1.Concatenate NumPy array to another array


In this Python program example, we will learn how to Concatenate function to Concatenate NumPy array to another array.

  • First import the numpy library using “import numpy as np”.
  • We have created two numpy array using nparr1,nparr2 using np.array().
  • We simply using the concatenate() function to append one numpy array to another array.
import numpy as np
nparr1 = np.array([[3, 6, 9], [2, 4, 6]])

nparr2 = np.array([[4, 8, 12], [6, 12, 18]])

Resultarr = np.concatenate((nparr1, nparr2))

print(Resultarr)

Output

[[ 3  6  9]
 [ 2  4  6]
 [ 4  8 12]
 [ 6 12 18]]

2.np.append() to concatenate NumPy array to another array


In this Python program example,we will discuss how to Concatenate NumPy array to another array using append() function

  • First import the numpy library using “import numpy as np”.
  • We have created two numpy array using nparr1,nparr2 using np.array()
  • We simply using the append() function to append one numpy array to another array.
  • We can append numpy array to another array row-wise by using axis=0 and column-wise by using axis=1.
  • We are appending numpy array row-wise.

import numpy as np
nparr1 = np.array([[3, 6, 9], [2, 4, 6]])

nparr2 = np.array([[4, 8, 12], [6, 12, 18]])

Resultarr = np.append(nparr1, nparr2)
rowise_append = np.append(nparr1, nparr2,axis=0)

print(Resultarr)
print('row-wise append:\n ',rowise_append)

Output

[ 3  6  9  2  4  6  4  8 12  6 12 18]
row-wise append:
  [[ 3  6  9]
 [ 2  4  6]
 [ 4  8 12]
 [ 6 12 18]]

3. vstack() to Concatenate NumPy array to another array


In this Python program, we will learn how to Concatenate the NumPy array to another array by using the numpy.vstack() function stack sequence of array vertically (row-wise).

  • First import numpy library using “import numpy as np”.
  • Created two numpy array using nparr1,nparr2 using np.array()
  • Using the stack() function to append one numpy array to another array.
  • We are appending numpy array row-wise using vstack().
import numpy as np
nparr1 = np.array([[3, 6, 9], [2, 4, 6]])

nparr2 = np.array([[4, 8, 12], [6, 12, 18]])

Resultarr = np.stack([nparr1, nparr2])

print(Resultarr)


Output

[[[ 3  6  9]
  [ 2  4  6]]

 [[ 4  8 12]
  [ 6 12 18]]]

4. hstack() to Concatenate NumPy array to another array


In this example, we will how to append the NumPy array to another array by using the numpy.hstack() function that stack sequence of array vertically (column-wise).

  • First import numpy library using “import numpy as np”.
  • Created two numpy array using nparr1,nparr2 using np.array()
  • Using hstack() function to append one numpy array to another array.
  • We are appending numpy array column-wise using vstack().
import numpy as np
nparr1 = np.array([[3, 6, 9], [2, 4, 6]])

nparr2 = np.array([[4, 8, 12], [6, 12, 18]])

Resultarr = np.hstack([nparr1, nparr2])

print(Resultarr)

Output

[[ 3  6  9  4  8 12]
 [ 2  4  6  6 12 18]]

5.column_stack() to concatenate NumPy array to another array


In this Python program example, we will use column_stack() to append a numpy array to another array.The column_stack() function stack 1 D numpy as to 2D numpy array.

import numpy as np
nparr1 = np.array([[3, 6, 9], [2, 4, 6]])

nparr2 = np.array([[4, 8, 12], [6, 12, 18]])

Resultarr = np.column_stack([nparr1, nparr2])

print(Resultarr)

Output

[[ 3  6  9  4  8 12]
 [ 2  4  6  6 12 18]]

Summary

In this post we have learned how to concatenate NumPy array to another array by using built in numpy function np.concatenate() ,np.append(),np.stack(),np.hstack(), column_stack().