How to insert column in Numpy array : Various Methods

Insert a column in Numpy array using Various Methods

Numpy is the best python package for performing array creation and complex mathematical calculations. All these calculations are done using the various inbuilt functions. In this entire tutorial, you will learn the various methods to insert a column in the NumPy array.

Create a Sample Array

Before going to the methods part let’s first create two samples NumPy array. One is the existing array and the other is the array you want to insert a column in the first array.

In NumPy, you can create a NumPy array using the np.array() functions. Let’s create them.

import numpy as np
array = np.array([[10,20,30],[40,50,60],[70,80,90]])
array2 = np.array([100,110,120])

How to insert column in numpy array

In this section, you will know the various methods t insert column in the NumPy array.

Method 1: Using the numpy.c_

This method will use the np.c_[array1,array2] to insert column in NumPy array. It inserts elements of the array as a stack. You can compare it with numpy.vstack. However, instead of round bracket ‘(), you have to use square bracket ‘[]’.

Complete Code

import numpy as np
array = np.array([[10,20,30],[40,50,60],[70,80,90]])
array2 = np.array([100,110,120])
added_array = np.c_[array,array2]
print(added_array)

Output

Insert column in numpy using numpy c_
Insert a column in NumPy using numpy.c_

Method 2: Insert a column in NumPy array using NumPy vstack function

The second method to insert a column in the array is using the np.vstack function. It allows you to Stack arrays in sequence vertically (row-wise).

Execute the below lines of code to insert a column to array,

import numpy as np
array = np.array([[10,20,30],[40,50,60],[70,80,90]])
array2 = np.array([100,110,120])
added_array = np.vstack([array,array2]).T
print(added_array)

Output

Insert column in numpy using numpy vstack method
Insert a column in using np.vstack method

Method 3: Using the numpy.insert() function

The next method to insert a column to the existing array is through np.insert() method. You have to pass four parameters for that. One is the existing array, second is the index where you want to add a column(in our case it is 3), third is the array you want to insert and the last parameter is the axis (It is 1 for columns).

Execute the below lines of code.

import numpy as np
array = np.array([[10,20,30],[40,50,60],[70,80,90]])
array2 = np.array([100,110,120])
added_array = np.insert(array,3,array2,axis=1)
print(added_array)

Output

Insert column in numpy using numpy insert method
Insert a column using insert() method

Conclusion

Numpy can do any complex mathematical calculation where it is an array or  vectors. These are the methods to insert columns in the NumPy array.

I hope you have liked this tutorial. If you want to get more information then please contact us. Our team is always ready to help you.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner