Articles

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]]

[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
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)
[32],
[56]])

print()

#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
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)
[32],
[56]])

print()

#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)

#add this array to the actual (above array)

#add this array to the actual (above array)

print()

#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

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)

print()

#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

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)

print()

#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