w3resource

NumPy: Access last two columns of a multidimensional columns

NumPy: Array Object Exercise-104 with Solution

Write a NumPy program to access last two columns of a multidimensional columns.

Sample Solution:

Python Code:

import numpy as np
arra = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arra)
result = arra[:,[1,2]]
print(result)

Sample Output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[2 3]
 [5 6]
 [8 9]]

Pictorial Presentation:

Python NumPy: Access last two columns of a multidimensional columns

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to calculate the Euclidean distance.
Next: Write a NumPy program to read a CSV data file and store records in an array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}