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.

Advertisement
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]
Advertisement

Learn the concept

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

More numpy programs

← All 100 NumPy Programs