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.

Advertisement
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

Leave a Comment