Articles

Add Row to numpy Array Python

Add Row to numpy Array Python


In this numpy tutorial, we will discuss how to add row to numpy array python.

 

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,43,22]

Result array:

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

Method - 1 : add row to numpy array python using append()

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

Syntax: add row to numpy array

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

where,

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

Example: add row to numpy array

In this add row to numpy array example, we will create an 2D array with three rows and add a row 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,22])

print()

#add
array_input = numpy.append(array_input, [adding_array], axis=0)

#display the final array
print(array_input)

Output:

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

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

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

Method - 2 : add row to numpy array using concatenate()

We can add a row to an existing numpy array by using concatenate() method. We can also add multiple rows 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=0 specifies row.

Example 1: add row to numpy array

In this add row to numpy array python example, we will create an 2D array with three rows and add a row 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,22])

print()

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

#display the final array
print(array_input)

Output:

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

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

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

Example 2: add row to numpy array python

In this add row to numpy array python example, we will create an 2D array with three rows and add 3 rows 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,22])

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

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

print()

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

#display the final array
print(array_input)

Output:

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

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

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

Method - 3 : add row to numpy array python using vstack()

We can add a row to an existing numpy array by using vstack() method. vstack stands for vertical stacking. It will add the new rows 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: add row to numpy array

In this example, we will create an 2D array with three rows and add a row 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,22])

print()

#add
array_input = numpy.vstack((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 row with vstack() method.

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

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

Numpy

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!