Numpy arrays are very versatile when it comes to manipulating and extracting values. In this tutorial, we will look at how to get the first column of a two-dimensional Numpy array with the help of some examples.
Steps to get the first column of a Numpy Array
Let’s look at a step-by-step example of how to extract the first column from a two-dimensional Numpy array.
Step 1 – Create a 2D Numpy array
First, we will create a 2D Numpy array that we will use throughout this tutorial.
import numpy as np # create 2D Numpy array ar = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # display the array print(ar)
Output:
[[1 2 3] [4 5 6] [7 8 9]]
Here, we used the numpy.array()
function to create a two-dimensional Numpy array having three rows and three columns.
You can see that the first column of the above array contains the values 1, 4, and 7
Step 2 – Slice the array to get the first column
You can use slicing to extract the first column of a Numpy array. The idea is to slice the original array for all the rows and just the first column (which has a column index of 0).
For example, to get the first column of the array ar
use the syntax ar[:, 0]
.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s get the first column of the array created above.
# get the first column ar[:, 0]
Output:
array([1, 4, 7])
We get a Numpy array of the values in the first column of our original array (ar
). Note that the resulting array, here, is one-dimensional.
ar[:, 0].shape
Output:
(3,)
If you instead want the first column as a Numpy array with shape (3, 1)
(like a column vector) use the following syntax –
# get the first column ar[:, [0]]
Output:
array([[1], [4], [7]])
We get the first column from the original array as a separate column (sort of like a column vector). Let’s see its shape.
ar[:, [0]].shape
Output:
(3, 1)
You can see that the resulting array is of shape (3, 1)
Summary
In this tutorial, we looked at how to extract the first column of a two-dimensional Numpy array. The following is a short summary of the steps mentioned in this tutorial.
- Create a 2D Numpy array (skip this step if you already have an array to operate on).
- Use slicing to get the first column of the array –
- If you want the values in the first column as a simple one-dimensional array, use the syntax
ar[:, 0]
. - If you want the resulting values in a column vector (for example, with shape
(n, 1)
where n is the total number of rows), use the syntaxar[: [0]]
.
- If you want the values in the first column as a simple one-dimensional array, use the syntax
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.