Drop rows with NaN

Python Practice › NumPy Programs › Program 76

Drop rows with NaN

EasyNumPy Programs · 76 of 100

Write a NumPy program to remove rows that contain NaN.

Input
[[4, nan, 8], [2, 4, 6]]
Output
[[2. 4. 6.]]

Approach

  1. Keep rows where no element is NaN.

Solution

Python
import numpy as npa = np.array([[4, np.nan, 8], [2, 4, 6]])print(a[~np.isnan(a).any(axis=1)])
Output
[[2. 4. 6.]]

Line by line

Line 3

isnan(a).any(axis=1) is True for rows with a NaN; ~ inverts it.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Replace negative values

Python Practice › NumPy Programs › Program 75

Replace negative values

EasyNumPy Programs · 75 of 100

Write a NumPy program to replace negative values with 1.

Input
[[4, 8, -5], [2, -9, 6]]
Output
[[4 8 1]
 [2 1 6]]

Approach

  1. Assign through a mask.

Solution

Python
import numpy as npa = np.array([[4, 8, -5], [2, -9, 6]])a[a < 0] = 1print(a)
Output
[[4 8 1]
 [2 1 6]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Add a column

Python Practice › NumPy Programs › Program 74

Add a column

EasyNumPy Programs · 74 of 100

Write a NumPy program to add an extra column to an array.

Input
[[4, 8, 0], [2, 0, 6]], column [5, 7]
Output
[[4 8 0 5]
 [2 0 6 7]]

Approach

  1. np.column_stack or np.hstack with a reshaped column.

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Float array to integers

Python Practice › NumPy Programs › Program 73

Float array to integers

EasyNumPy Programs · 73 of 100

Write a NumPy program to convert an array of floats to integers.

Input
[[6.7, 5.2], [9.4, 2.7]]
Output
[[6 5]
 [9 2]]
[[7 5]
 [9 3]]

Approach

  1. astype(int) truncates; round first if needed.

Solution

Python
import numpy as npa = np.array([[6.7, 5.2], [9.4, 2.7]])print(a.astype(int))print(np.rint(a).astype(int))
Output
[[6 5]
 [9 2]]
[[7 5]
 [9 3]]

Line by line

Line 3

Truncation drops the decimals.

Line 4

rint rounds to the nearest integer first.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Access by column

Python Practice › NumPy Programs › Program 72

Access by column

EasyNumPy Programs · 72 of 100

Write a NumPy program to print each column of an array.

Input
[[4, 8, 0], [2, 0, 6]]
Output
Column 1: [4 2]
Column 2: [8 0]
Column 3: [0 6]

Approach

  1. a[:, j] selects column j.

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for j in range(a.shape[1]):    print(f"Column {j + 1}:", a[:, j])
Output
Column 1: [4 2]
Column 2: [8 0]
Column 3: [0 6]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Square every element

Python Practice › NumPy Programs › Program 71

Square every element

EasyNumPy Programs · 71 of 100

Write a NumPy program to print the squares of all elements.

Input
[2, 6, 3, 1]
Output
[ 4 36  9  1]

Approach

  1. a ** 2 or np.square.

Solution

Python
import numpy as npprint(np.square(np.array([2, 6, 3, 1])))
Output
[ 4 36  9  1]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Print every element

Python Practice › NumPy Programs › Program 70

Print every element

EasyNumPy Programs · 70 of 100

Write a NumPy program to print every element of a 2-D array.

Input
[[4, 8, 0], [2, 0, 6]]
Output
4 8 0 2 0 6 

Approach

  1. np.nditer or ravel().

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for x in np.nditer(a):    print(x, end=" ")print()
Output
4 8 0 2 0 6 

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Make an array read-only

Python Practice › NumPy Programs › Program 69

Make an array read-only

EasyNumPy Programs · 69 of 100

Write a NumPy program to make an array immutable.

Output
Error: assignment destination is read-only

Approach

  1. Set flags.writeable = False.

Solution

Python
import numpy as npa = np.array([1, 2, 3])a.flags.writeable = Falsetry:    a[0] = 10except ValueError as e:    print("Error:", e)
Output
Error: assignment destination is read-only

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Median of each column

Python Practice › NumPy Programs › Program 68

Median of each column

EasyNumPy Programs · 68 of 100

Write a NumPy program to find the median of each column.

Input
[[4, 8, 1], [2, 9, 6]]
Output
[3.  8.5 3.5]

Approach

  1. axis=0.

Solution

Python
import numpy as npprint(np.median(np.array([[4, 8, 1], [2, 9, 6]]), axis=0))
Output
[3.  8.5 3.5]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Median of each row

Python Practice › NumPy Programs › Program 67

Median of each row

EasyNumPy Programs · 67 of 100

Write a NumPy program to find the median of each row.

Input
[[4, 8, 1], [2, 9, 6]]
Output
[4. 6.]

Approach

  1. axis=1.

Solution

Python
import numpy as npprint(np.median(np.array([[4, 8, 1], [2, 9, 6]]), axis=1))
Output
[4. 6.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs