Home > Backend Development > Python Tutorial > How do you access specific columns in a NumPy multidimensional array?

How do you access specific columns in a NumPy multidimensional array?

Patricia Arquette
Release: 2024-10-27 11:53:01
Original
626 people have browsed it

How do you access specific columns in a NumPy multidimensional array?

Accessing Columns in NumPy Multidimensional Arrays

Given a NumPy multidimensional array, retrieving specific columns can be achieved efficiently using indexing techniques. To access the ith column, utilize the following syntax:

<code class="python">array[:, i]</code>
Copy after login

For example:

<code class="python">test = np.array([[1, 2], [3, 4], [5, 6]])
test[:, 0]  # Accesses the first column</code>
Copy after login

which outputs:

array([1, 3, 5])
Copy after login

Conversely, to access the ith row, use:

<code class="python">array[i, :]</code>
Copy after login

For example:

<code class="python">test[0, :]  # Accesses the first row</code>
Copy after login

which outputs:

array([1, 2])
Copy after login

Refer to the NumPy reference's Indexing section for further details. This operation is generally efficient, particularly in comparison to looping over individual elements.

The above is the detailed content of How do you access specific columns in a NumPy multidimensional array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template