Python Practice › NumPy Programs › Program 74
Add a column
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
np.column_stackornp.hstackwith a reshaped column.
Solution
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.