Access by column
Write a NumPy program to print each column of an array.
[[4, 8, 0], [2, 0, 6]]
Column 1: [4 2] Column 2: [8 0] Column 3: [0 6]
Approach
a[:, j]selects column j.
Solution
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])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.