The Numpy library in Python comes with a number of useful methods and techniques to work with and manipulate data in arrays. In this tutorial, we will look at how to get the first N columns of a two-dimensional Numpy Array with the help of some examples.
How to get the first n columns of a 2D Numpy array?

You can use slicing to get the first N columns of a 2D array in Numpy. Here, we use column indices to specify the range of columns that we’d like to slice. To get the first n columns, use the following slicing syntax –
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
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.
# first n columns of numpy array ar[:, :n]
It returns the first n columns (including all the rows) of the given array.
Steps to get the first n columns of 2D array
Let’s now look at a step-by-step example of using the above syntax on a 2D Numpy array.
Step 1 – Create a 2D Numpy array
First, we will create a 2D Numpy array that we’ll operate on.
Upskill your career right now →
import numpy as np # create a 2D array ar = np.array([ ['Tim', 181, 86], ['Peter', 170, 68], ['Isha', 158, 59], ['Rohan', 168, 81], ['Yuri', 171, 65], ['Emma', 166, 64], ['Michael', 175, 78], ['Jim', 190, 87], ['Pam', 168, 57], ['Dwight', 187, 84] ]) # display the array print(ar)
Output:
[['Tim' '181' '86'] ['Peter' '170' '68'] ['Isha' '158' '59'] ['Rohan' '168' '81'] ['Yuri' '171' '65'] ['Emma' '166' '64'] ['Michael' '175' '78'] ['Jim' '190' '87'] ['Pam' '168' '57'] ['Dwight' '187' '84']]
Here, we used the numpy.array()
function to create a 2D Numpy array with 10 rows and 3 columns. The array contains information on the height (in cm) and weight (in kg) of some employees in an office.
Step 2 – Slice the array to get the first n columns
To get the first n columns of the above array, slice the array starting from the first column (0th index) up to (but not including) the column with the nth index.
For example, let’s get the first 2 columns from the array that we created in step 1.
# first 2 columns print(ar[:, 0:2])
Output:
[['Tim' '181'] ['Peter' '170'] ['Isha' '158'] ['Rohan' '168'] ['Yuri' '171'] ['Emma' '166'] ['Michael' '175'] ['Jim' '190'] ['Pam' '168'] ['Dwight' '187']]
We get the first 2 columns of the 2D array.
In the above code, you don’t need to explicitly specify 0 (since you’re slicing starting from the very first column of the array).
Upskill your career right now →
# first 2 columns print(ar[:, :2])
Output:
[['Tim' '181'] ['Peter' '170'] ['Isha' '158'] ['Rohan' '168'] ['Yuri' '171'] ['Emma' '166'] ['Michael' '175'] ['Jim' '190'] ['Pam' '168'] ['Dwight' '187']]
We get the same result as above.
For more on slicing Numpy arrays, refer to its documentation.
Summary
In this tutorial, we looked at how to get the first n columns of a two-dimensional Numpy array using slicing. The following is a short summary of the steps mentioned in the tutorial.
- Create a 2D Numpy array (skip this step if you already have an array to operate on).
- Slice the array from the column with index 0 to the column with index n (but not including it) to get the first n columns of the array.
You might also be interested in –
- Numpy – Get Max Value in Array
- Get the Median of Numpy Array – (With Examples)
- Pandas – Select first n rows of a DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.