How to get first n rows of NumPy array

In this post, we are going to learn how to get the first n rows of the NumPy array with examples. To select the first N rows and first n element from the NumPy array slicing and indexing is used. We will cover getting the first n element by using slicing, selecting the first n row based on indices, selecting the N row by using the np.r_ () function, get the first element of the NumPy array. To run all these programs make sure the NumPy library is Installed on the local machine.

1. How to get first N rows of NumPy array


To select the first n row of the NumPy array using slicing. We use Index brackets ([]) to select rows from the NumPy array. We can select single or multiple rows using this syntax. To select a single row by index we use this syntax.

ndarray[rowindex]

To select multiple rows by index we use this syntax

Syntax

ndarray[Startindex : EndIndex, : ]

Parameters

  • StartIndex : EndIndex: It will select the row start from startindex till to EndIndex-1.
  • colon(:): It will select all columns of the given array
numpyarry[row  ,:] 

In this example, we are Selecting the first five rows from the NumPy array, and all columns of first five rows by using code origanlArr[0:5, :] .This is how to get first n rows of NumPy array.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print('select first firve rows: \n',origanlArr[0:5, :])

Output

select first two rows: 
 [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]]

2. Get First n elements of NumPy array


In this python program,We are selecting few elements from the NumPy array by using list comprehension and creating a new array with elements from the start of the third column in all rows of the NumPy array.We have specify the dtype of NumPy array elements,We have to change as per given array datatype . If it contains mixed datatype ‘object’ would be use.This is how we Get First few elements of NumPy array.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

resArr = np.array([item[3:] for item in origanlArr], dtype=int)

print(resArr)

Output

[[12 15 18 21]
 [28 35 42 49]
 [44 55 66 77]
 [48 60 72 84]
 [36 45 54 63]
 [40 50 60 70]]

3. How to Get specific n row from Numpy array by index


We have used slicing in above example to select row in given range,However sometimes it need to select specific row from the NumPy array or we know indices of rows.In this python example we are selecting row by index (1,2) and all columns of rows at indices 1,2 by using colon(:).Let us understand with below example.

  • Import NumPy module using “import numpy as np”
  • select first n rows from NumPy array by using index brackets and all columns of selected rows using colon(:)
import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print(origanlArr[[1,2],:])  

Output

[[ 4  5  6  7]
 [ 8  9 10 11]]

4. How to get first 5 rows of NumPy array


In this python program example, we are using the NumPy library the np.r_ () function that Translates slice objects to concatenation along the first axis. If we use slicing notation[start:stop: step] it works like the np.arange() function. It will select the first 5 rows from the given NumPy array.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print(origanlArr[np.r_[0:5],:])  

Output

[[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]]

Summary

In this post, we have learned how to get first n rows of NumPy array by using slicing, indexing, and np.r_() function with examples.