How to Add Columns to NumPy Array in Python

In this article, we will learn how to add a column to a NumPy Array in Python.

Table Of Contents

Given a 2D NumPy Array, we need to add a Column to the array.

Given array:

[[1, 2, 3, 4, 5],
 [5, 4, 3, 2, 1]]
 
New Column:
[[6],
 [0]]

After adding Column to the array:

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

There are multiple ways to Add a Column to a NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example

1.) Using append() method to Add a Column to a NumPy Array

Numpy module in python, provides a function numpy.append() to add objects to the end of an array. The object should be an array like entity. The append() method will take an array and object to be appended as arguments. It will append the given object at the end of the copy of given array and returns the new array with the appended data.

Syntax of append()

numpy.append(arr, values, axis=None)

Parameters:
	arr          = The array to be passed to the function.
	values       = array_like object to appended to the array.
	axis         = int, optional, Axis along which to append values.

Return:
	Returns array with values appended at the end.
    

In this case, to add a column to a 2D NumPy Array, we need to pass the numpy array and Column to the append() method and set the axis = 1. It will return the array by adding the Column.

Advertisements

Approach

  • Import numpy library and create a numpy array
  • Now pass the array, Column to be added to the append() method and set axis = 1.
  • The method will return copy of the array by adding the Column.
  • Print the new array

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

# The column to be added
col = np.array([[6],
                [0]])

# Adding column to array using append() method
arr = np.append(arr, col, axis=1)

# Array after adding the column.
print(arr)

OUTPUT:

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

2.) Using concatenate() method to add a column to NumPy Array

The Numpy module in python, provides a function numpy.concatenate() to join a sequence of arrays along an existing axis. The concatenate() method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the concatenated array.

Now to Add a Column to a NumPy Array, pass the given array and the column to be added and axis = 1 to the concatenate() method. It will return the a copy of array with the new column added in it.

Syntax of concatenate()

numpy.concatenate((a1, a2, ...), axis=0)

Parameters:

	(a1, a2, ...) = Sequence of arrays to be passed to the function.
	axis          = int, optional, Axis along which to concatenate arrays.

Returns:

	Returns a concatenated array.

Approach

  1. Import numpy library and create a numpy array
  2. Now pass the array and Column to be added as a sequence of arrays, axis = 1 to the concatenate method
  3. The method will return a copy of the array with the Column added to it.
  4. Print the new array

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

# The column to be added
col = np.array([[6],
                [0]])

# Adding column to array using concatenate()
arr = np.concatenate([arr, col], axis=1)

# Array after adding the column.
print(arr)

OUTPUT:

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

3.) Using insert() method to add a column to NumPy Array

The Numpy module in python, provides a function numpy.insert() to insert values along the given axis before the given index. The insert() method will take an array, an index and values to be inserted as parameters. It will insert the given value just before the specified index in a copy of array and returns that modified copy of array.

Now, to add a column to a NumPy Array, we need to pass the array, index, and values to be inserted as column to the insert() method and also set the axis = 1. Here we are adding Column at front of the array so let’s give index = 0.

Syntax of insert()

numpy.insert(arr, obj, values, axis=None)

Parameters:
	arr          = The array to be passed to the function.
	obj          = index at which value needs to be inserted
	values       = Values or object to insert into array.
	axis         = int, optional, Axis along which to insert values.

Returns:
	Returns array with value inserted at the specified index, in this case appended at the end of the array.

Approach

  • Import numpy library and create numpy array
  • Now pass the array, Column to be inserted and index = 0, axis = 1 to the insert() method
  • That’s it. The insert() method will return a copy of the array with the Column added.
  • Print the new array.

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

# The column to be added
col = np.array([6,0])

# Adding column at the starting of 2D NumPy Array
arr = np.insert(arr, 0, col, axis=1)

# Array after adding the column.
print(arr)

OUTPUT:

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

4.) Using column_stack() to add a column to 2D NumPy Array

Numpy module in python, provides a function numpy.column_stack() function is used to stack arrays in Column-wise. i.e, The column_stack() method will take a sequence of 1-D arrays and stack them as columns to make a single 2-D array and returns the array. The column_stack() is similar to h_stack(). The column_stack() method first converts the 1-D arrays passed to it into 2-D columns.

Now to add a column to a NumPy Array, in the sequence, we will pass the given array and the column to be added to the column_stack() method. It will return the array with the column added.

Syntax of column_stack()

numpy.column_stack(tuple)

Parameters:
	tuple = sequence of arrays to be passed to the function.

Returns:
	Returns The array formed by stacking the given arrays.
    

Approach

  1. Import numpy library and create numpy array
  2. Now pass the array, Column to be inserted as a sequence of arrays to the column_stack method
  3. That’s it , The column_stack() method will return a copy of the array with the Column added.
  4. Print the new array.

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

# The column to be added
col = np.array([6,0])
        
# Adding column to 2D NumPy Array
arr = np.column_stack((arr,col))

# Array after adding the column.
print(arr)

OUTPUT:

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

5.) Using hstack() to add a column to 2D NumPy Array

Numpy module in python, provides a function numpy.hstack() function is used to stack the sequence of input arrays horizontally i.e, concatenating into a single array. The hstack() method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the array. The hstack() is equivalent to concatenation.

Now to add a column to a NumPy Array, in the sequence of arrays, pass the given array and the column to be added. The hstack() method will return the array with the column added.

Syntax of hstack()

numpy.hstack(tuple)

Parameters:
	tuple = Sequence of arrays to be passed to the function.

Returns:
	Returns The array formed by stacking the given arrays.
    

Approach

  1. Import numpy library and create numpy array
  2. Now pass the array, Column to be inserted as a sequence of arrays to the hstack method
  3. That’s it , The hstack() method will return a copy of the array with the Column added.
  4. Print the new array.

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

# The column to be added
col = np.array([[6],[0]])
        
# Adding a column to a 2D NumPy Array
arr = np.hstack((arr, col))

# Array after adding the column.
print(arr)

OUTPUT:

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

Summary

Great! you made it, We have discussed all possible methods to Add a Column to a NumPy Array in Python. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top