
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Access Different Columns of a Multidimensional NumPy Array
Numpy is the most powerful library in python for computing numerical data. It provides multidimensional arrays, along with different collections of functions and modules to work with arrays. Its efficient array operations, broadcasting capabilities, and integration with other libraries make it a go-to choice for data manipulation, analysis, and modeling tasks. The following are the key features and functionalities of the Numpy library.
Multidimensional arrays
Array creation
Array operations
Indexing and Slicing
Vectorized operations
Numerical Routines
Integration with other libraries
Performance
Open source and community support
Creation of an array
In Numpy library we have the functions called array() and reshape(). In which array() function creates the array in one dimension and reshape() function converts the given list of elements into defined shape.
Example
In this example we will create the 2-d array using the array() function by passing the list of elements and reshape() function by passing the shape of the array i.e. number of rows and columns.
import numpy as np l = [90,56,14,22,1,21,7,12,5,24] arr = np.array(l).reshape(2,5) print("array:",arr) print("dimension of the array:",np.ndim(arr))
Output
array: [[90 56 14 22 1] [21 7 12 5 24]] dimension of the array: 2
There are several approaches to access different columns of Multidimensional arrays. Let's see each one in detail.
Using Basic Indexing
We can use basic indexing with square brackets to access specific columns of a NumPy array, in which we specify the column index within the brackets to retrieve the desired column or columns.
Example
In this example we are applying the basic indexing approach to the 2-d array for accessing the first column of the array using [:,0] then it returns the first column elements of the 2-d array.
import numpy as np l = [90,56,14,22,1,21,7,12,5,24] arr = np.array(l).reshape(2,5) print("array:",arr) print("dimension of the array:",np.ndim(arr)) print("The first column of the array:",arr[:,0])
Output
array: [[90 56 14 22 1] [21 7 12 5 24]] dimension of the array: 2 The first column of the array: [90 21]
Using slicing
Slicing is used to access a range of columns from the given input array, in which we have to specify the start and end indices, along with the step size separated by colons.
Example
In this example we are accessing the middle 2 columns of the input array by using the slicing technique by applying [:,3:5] and it returns the middle column elements as the output.
import numpy as np l = [90,56,14,22,1,21,7,12,5,24] arr = np.array(l).reshape(2,5) print("array:",arr) print("dimension of the array:",np.ndim(arr)) print("The middle columns of the array:",arr[:,3:5])
Output
array: [[90 56 14 22 1] [21 7 12 5 24]] dimension of the array: 2 The middle columns of the array: [[22 1] [ 5 24]]
Using Fancy Indexing
Fancy indexing allows us to access specific columns by providing an array of column indices. We can use this approach when we want to access non-consecutive columns or a specific subset of columns.
Example
Here in this example we are accessing the columns first and last by using the fancy indexing with applying list of indices [:,0,4] then it returns the last and first column elements of the array.
import numpy as np l = [90,56,14,22,1,21,7,12,5,24] arr = np.array(l).reshape(2,5) print("array:",arr) print("dimension of the array:",np.ndim(arr)) print("The first and last columns of the array:",arr[:,[0,4]])
Output
array: [[90 56 14 22 1] [21 7 12 5 24]] dimension of the array: 2 The first and last columns of the array: [[90 1] [21 24]]