Add one row to another

Python Practice › NumPy Programs › Program 52

Add one row to another

EasyNumPy Programs · 52 of 100

Write a NumPy program to add row 2 to row 1.

Input
[[6, 5], [10, 9], [7, 8]]
Output
[[16 14]
 [10  9]
 [ 7  8]]

Approach

  1. a[0] += a[1].

Solution

Python
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])a[0] += a[1]print(a)
Output
[[16 14]
 [10  9]
 [ 7  8]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Multiply a row by a scalar

Python Practice › NumPy Programs › Program 51

Multiply a row by a scalar

EasyNumPy Programs · 51 of 100

Write a NumPy program to multiply row 1 of an array by 2.

Input
[[6, 5], [10, 9], [7, 8]]
Output
[[12 10]
 [10  9]
 [ 7  8]]

Approach

  1. Index the row and use *=.

Solution

Python
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])a[0] *= 2print(a)
Output
[[12 10]
 [10  9]
 [ 7  8]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Reverse the row order

Python Practice › NumPy Programs › Program 50

Reverse the row order

EasyNumPy Programs · 50 of 100

Write a NumPy program to swap the rows of an array (first and last).

Input
[[6, 5], [10, 9], [7, 8]]
Output
[[ 7  8]
 [10  9]
 [ 6  5]]

Approach

  1. Fancy indexing with a new row order.

Solution

Python
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])a[[0, 2]] = a[[2, 0]]print(a)
Output
[[ 7  8]
 [10  9]
 [ 6  5]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Replace numbers at or above a value

Python Practice › NumPy Programs › Program 49

Replace numbers at or above a value

EasyNumPy Programs · 49 of 100

Write a NumPy program to replace numbers greater than or equal to 8 with 0.

Input
[[6, 5], [10, 9], [7, 8]]
Output
[[6 5]
 [0 0]
 [7 0]]

Approach

  1. np.where(condition, new, old).

Solution

Python
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])print(np.where(a >= 8, 0, a))
Output
[[6 5]
 [0 0]
 [7 0]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Numbers greater than a value

Python Practice › NumPy Programs › Program 48

Numbers greater than a value

EasyNumPy Programs · 48 of 100

Write a NumPy program to extract numbers greater than 6.

Input
[[6, 5], [10, 9], [7, 8]]
Output
[10  9  7  8]

Approach

  1. A boolean mask.

Solution

Python
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])print(a[a > 6])
Output
[10  9  7  8]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sort rows by the first column

Python Practice › NumPy Programs › Program 47

Sort rows by the first column

EasyNumPy Programs · 47 of 100

Write a NumPy program to sort the rows of an array by the first column.

Input
[[6, 5], [10, 9], [7, 8]]
Output
[[ 6  5]
 [ 7  8]
 [10  9]]

Approach

  1. argsort on the column gives the row order.

Solution

Python
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])print(a[a[:, 0].argsort()])
Output
[[ 6  5]
 [ 7  8]
 [10  9]]

Line by line

Line 3

a[:, 0] is the first column; its argsort is the order in which to take the rows.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sort each row

Python Practice › NumPy Programs › Program 46

Sort each row

EasyNumPy Programs · 46 of 100

Write a NumPy program to sort each row in ascending order.

Input
[[6, 5], [10, 9], [8, 7]]
Output
[[ 5  6]
 [ 9 10]
 [ 7  8]]

Approach

  1. np.sort(a, axis=1).

Solution

Python
import numpy as npprint(np.sort(np.array([[6, 5], [10, 9], [8, 7]]), axis=1))
Output
[[ 5  6]
 [ 9 10]
 [ 7  8]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Transpose a matrix

Python Practice › NumPy Programs › Program 45

Transpose a matrix

EasyNumPy Programs · 45 of 100

Write a NumPy program to swap the rows and columns of a matrix.

Input
[[5, 6], [7, 8], [9, 10]]
Output
[[ 5  7  9]
 [ 6  8 10]]

Approach

  1. .T.

Solution

Python
import numpy as npprint(np.array([[5, 6], [7, 8], [9, 10]]).T)
Output
[[ 5  7  9]
 [ 6  8 10]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

One-dimensional array

Python Practice › NumPy Programs › Program 44

One-dimensional array

EasyNumPy Programs · 44 of 100

Write a NumPy program to create a one-dimensional array.

Output
[65 90  1  3  5] 1

Approach

  1. np.array with a flat list.

Solution

Python
import numpy as npa = np.array([65, 90, 1, 3, 5])print(a, a.ndim)
Output
[65 90  1  3  5] 1

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise equality

Python Practice › NumPy Programs › Program 43

Element-wise equality

EasyNumPy Programs · 43 of 100

Write a NumPy program to check whether two arrays are equal element-wise.

Input
[5, 3], [7, 8]
Output
[False False]

Approach

  1. np.equal.

Solution

Python
import numpy as npprint(np.equal([5, 3], [7, 8]))
Output
[False False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs