Articles

How to Add Columns to numpy Array in Python

How to Add Columns to numpy Array in Python


In this numpy tutorial,  we will discuss how to add a column to an numpy array.

Introduction

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python. 

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.

Array

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.

Scenario:

array_input=[[1,2,3,4],
                     [5,6,43,2]]

add an array - [[45],
                 [65]]

Result array:

array_input - [[1,2,3,4,45],
                     [5,6,43,2,65]]

Method - 1 : using append()

We can add a column to an existing numpy array by using append() method.

Syntax:

numpy.append(array_input, adding_array, axis=1)

where,

  1. array_input is an actual input array
  2. adding_array is used to add this array to the array_input
  3. axis=1 specifies column.

Example:

In this example, we will create an 2D array with three rows and add a column to this array.

#import the numpy module
import numpy 

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#display array
print(array_input)

#add this array to the actual (above array)
adding_array=numpy.array([[43],
                          [32],
                          [56]])

print()

#add
array_input = numpy.append(array_input, adding_array, axis=1)

#display the final array
print(array_input)

Output:

From the above code, we created an 2 D array with integer values and adding a column with append() method.

[[ 1  2  3  4]
 [34 32 44 22]
 [43 45 32 56]]

[[ 1  2  3  4 43]
 [34 32 44 22 32]
 [43 45 32 56 56]]

Method - 2 : using concatenate()

We can add a column to an existing numpy array by using concatenate() method. We can also add multiple columns at a time  using this method

Syntax:

numpy.concatenate([array_input, adding_array,...............] axis=0)

where,

  1. array_input is an actual input array
  2. adding_array/s is used to add this array to the array_input
  3. axis=1 specifies column.

Example 1:

In this example, we will create an 2D array with three rows and add a column to this array.

#import the numpy module
import numpy 

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#display array
print(array_input)

#add this array to the actual (above array)
adding_array=numpy.array([[43],
                          [32],
                          [56]])

print()

#add
array_input = numpy.concatenate([array_input, adding_array], axis=1)

#display the final array
print(array_input)

Output:

From the above code, we created an 2 D array with integer values and adding a column with concatenate() method.

[[ 1  2  3  4]
 [34 32 44 22]
 [43 45 32 56]]

[[ 1  2  3  4 43]
 [34 32 44 22 32]
 [43 45 32 56 56]]

Example 2:

In this example, we will create an 2D array with three rows and add 3 columns to this array.

#import the numpy module
import numpy 

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#display array
print(array_input)

#add this array tot the actual (above array)
adding_array1=numpy.array([[43],[32],[22]])

#add this array to the actual (above array)
adding_array2=numpy.array([[5],[6],[7]])

#add this array to the actual (above array)
adding_array3=numpy.array([[11],[22],[44]])

print()

#add
array_input = numpy.concatenate([array_input, adding_array1, adding_array2, adding_array3], axis=1)

#display the final array
print(array_input)

Output:

From the above code, we created an 2 D array with integer values and adding three columns with concatenate() method.

[[ 1  2  3  4]
 [34 32 44 22]
 [43 45 32 56]]

[[ 1  2  3  4 43  5 11]
 [34 32 44 22 32  6 22]
 [43 45 32 56 22  7 44]]

Method - 3 : using hstack()

We can add a row to an existing numpy array by using hstack() method. hstack stands for horizontal stacking. It will add the new columns at the end of the array.

Syntax:

numpy.append((array_input, adding_array))

where,

  1. array_input is an actual input array
  2. adding_array is used to add this array to the array_input

Example:

In this example, we will create an 2D array with three rows and add a column to this array.

#import the numpy module
import numpy 

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#display array
print(array_input)

#add this array to the actual (above array)
adding_array=numpy.array([[43],[32],[22]])

print()

#add
array_input = numpy.hstack((array_input,adding_array))

#display the final array
print(array_input)

Output:

From the above code, we created an 2 D array with integer values and adding a column with hstack() method.

[[ 1  2  3  4]
 [34 32 44 22]
 [43 45 32 56]]

[[ 1  2  3  4 43]
 [34 32 44 22 32]
 [43 45 32 56 22]]

Method - 4 : using column_stack()

We can add a row to an existing numpy array by using column_stack() method. 

Syntax:

numpy.column_stack((array_input, adding_array))

where,

  1. array_input is an actual input array
  2. adding_array is used to add this array to the array_input

Example:

In this example, we will create an 2D array with three rows and add a column to this array.

#import the numpy module
import numpy 

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#display array
print(array_input)

#add this array to the actual (above array)
adding_array=numpy.array([[43],[32],[22]])

print()

#add
array_input = numpy.column_stack((array_input,adding_array))

#display the final array
print(array_input)

Output:

From the above code, we created an 2 D array with integer values and adding a column with column_stack() method.

[[ 1  2  3  4]
 [34 32 44 22]
 [43 45 32 56]]

[[ 1  2  3  4 43]
 [34 32 44 22 32]
 [43 45 32 56 22]]


Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 18,2023  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!