Python NumPy

Python Tutorial @ Home
Python Tutorial
Advance Python Tutorial

Python NumPy

What is Python NumPy
How to install Python NumPy
create a NumPy array
Add elements to an NumPy Array

Python Numpy 7

Append a row

In this section, we will be using the append() method to add a row to the array. It’s as simple as appending an element to the array. Consider the following example:

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.append(a, [[50, 60, 70]], axis = 0)
print(b)

The output will be as follows:

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print("Array a\n")
print(a)
b = np.append(a, [[50, 60, 70]], axis = 0)
print("Array b\n")
print(b)

c = np.append(b, [[100, 200, 300]], axis = 0)
print("Array c\n")
print(c)

Output:

Array a

[[1 2 3]
 [4 5 6]]
Array b

[[ 1  2  3]
 [ 4  5  6]
 [50 60 70]]
Array c

[[  1   2   3]
 [  4   5   6]
 [ 50  60  70]
 [100 200 300]]
>>> 

Delete a row

We can delete a row using the delete() method.
Consider the following example, where we have deleted a row from a 2-dimensional array:

import numpy as np
a=np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
b=np.delete(a, 1, axis = 0)
print(b)

Output:

[[ 1  2  3]
 [10 20 30]]
import numpy as np
a=np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
b=np.delete(a, 0, axis = 0)
print(b)

Output:

[[ 4  5  6]
 [10 20 30]]