How to Repeat Column in NumPy array

In this post, we are going to learn How to Repeat Columns in a NumPy array with examples. We have used the Numpy library built-in function transpose() and tile() and hstack() to repeat columns. To run and use the NumPy library it must be installed on a local machine. After installation, we can import it into our program. We will also cover repeating the last column of the 1D and 2D NumPy array.

1. How to Repeat Column in NumPy array using np.transpose()


In this Python program To repeat a column in the NumPy array, We have used the np.transpose() function [myarr] as a list . This function reverses the dimensions of the array and returns a modified array. It changes the row elements to column elements and column elements to row elements.

  • [myarr]: Is an array as a list
  • repeat_count is the number of times we want to repeat the array.
import numpy as np
 
myarr = np.array([ 0, 1, 2,3, 5 ,6 ])
 
print(myarr)

repeat_count = 2

resArr = np.transpose([myarr] * repeat_count)
print(resArr)

Output

[0 1 2 3 5 6]
[[0 0]
 [1 1]
 [2 2]
 [3 3]
 [5 5]
 [6 6]]

2. Repeat 2D NumPy array using np.transpose()


In this Python example, we are repeating columns in a 2D NumPy array by np.transpose().The step will remain the same as mentioned in the above example.

  • Import NumPy library as “import numpy as np”
  • Call np.transpose() with these parameters
    • [myarr]: Is an array in a list
    • repeat_count is the number of times we want to repeat the array.
import numpy as np
 
myarr = np.array([[0, 1, 2,3, 5],[3,6,9,12,15]])
 
print('original array :',myarr)

repeat_count = 2

resArr = np.transpose([myarr] * repeat_count) 
print('array after repetation:\n',resArr)

Output

original array : [[ 0  1  2  3  5]
 [ 3  6  9 12 15]]
array after repetation:
 [[[ 0  0]
  [ 3  3]]

 [[ 1  1]
  [ 6  6]]

 [[ 2  2]
  [ 9  9]]

 [[ 3  3]
  [12 12]]

 [[ 5  5]
  [15 15]]]

3. How to Repeat Column in NumPy array using tile()


The numpy.tile() method is used to repeat columns in NumPy 1D array. The first parameter is an array that columns we want to repeat and (repeat_count, 1) is a tuple that contains repeat_count that represents the number of times it repeats myarr.

import numpy as np
 
myarr = np.array([ 0, 1, 2,3, 5 ,6 ])
 
print('original array :',myarr)

repeat_count = 2

resArr = np.tile(myarr, (repeat_count, 1))
print('array after repetation:\n',resArr)

Output

original array : [0 1 2 3 5 6]
array after repetation:
 [[0 1 2 3 5 6]
 [0 1 2 3 5 6]]

4. Repeat Last in NumPy array


Sometimes we have the requirement to repeat the last column of the 1D NumPy array, We have used np.hstack() with np.tile() to repeat the last columns. The title() creates a new array by repeating the given array the number of times we want to repeat as per repetition.

import numpy as np
 
myarr = np.array([ 0, 1, 2,3, 5 ,6 ])
 
print('original array :',myarr)

repeat_count = 2

resArr = np.hstack((myarr, np.tile(myarr[[-1]], repeat_count)))
print('array after repetation:\n',resArr)

Output

original array : [0 1 2 3 5 6]
array after repetation:
 [0 1 2 3 5 6 6 6]

5. Repeat Last column in the 2D NumPy array


In this example, we are repeating a 2D NumPy array by using np.hstack() along with tile().Let us understand with the below example.

#python 3 program to repeat last column in 2D numpy array 
import numpy as np
 
myarr = np.array([[0, 1, 2,3, 5],[3,6,9,12,15]])
 
print('original array :',myarr)

repeat_count = 2

resArr = np.hstack((myarr, np.tile(myarr[:, [-1]], repeat_count)))
print('array after repetation:\n',resArr)

Output

original array : [[ 0  1  2  3  5]
 [ 3  6  9 12 15]]
array after repetation:
 [[ 0  1  2  3  5  5  5]
 [ 3  6  9 12 15 15 15]]

Summary

In this post, we have learned how to Repeat Columns in the NumPy array with examples by using transpose() and tile() and hstack() functions and also cover repeat last column of 1D and 2D array.