# How to randomly select rows of an array in Python with NumPy ?

• Last Updated : 25 Feb, 2021

In this article, we will see two different methods on how to randomly select rows of an array in Python with NumPy. Let’s see different methods by which we can select random rows of an array:

Method 1: We will be using the function shuffle(). The shuffle() function shuffles the rows of an array randomly and then we will display a random row of the 2D array.

## Python3

 `# import modules``import` `random``import` `numpy as np`` ` `# create 2D array``data ``=` `np.arange(``50``).reshape((``5``, ``10``))`` ` `# display original array``print``(``"Array:"``)``print``(data)`` ` `# row manipulation``np.random.shuffle(data)`` ` `# display random rows``print``(``"\nRandom row:"``)``rows ``=` `data[:``1``, :]``print``(rows)`

Output:

Method 2: First create an array, then apply the sample() method to it and display a single row.

## Python3

 `# import modules``import` `random``import` `numpy as np`` ` `# create 2D array``data ``=` `np.arange(``50``).reshape((``5``, ``10``))`` ` `# display original array``print``(``"Array:"``)``print``(data)`` ` `# row manipulation``rows_id ``=` `random.sample(``range``(``0``, ``                              ``data.shape[``1``]``-``1``), ``1``)`` ` `# display random rows``print``(``"\nRandom row:"``)``row ``=` `data[rows_id, :]``print``(row)`

Output:

Method 3: We will be using the function choice(). The choices() method returns multiple random elements from the list with replacement.

Now lets, select rows from the list of random integers that we have created.

## Python3

 `# import modules``import` `random``import` `numpy as np`` ` `# create 2D array``data ``=` `np.arange(``50``).reshape((``5``, ``10``))`` ` `# display original array``print``(``"Array:"``)``print``(data)`` ` `# row manipulation``number_of_rows ``=` `data.shape[``0``]``random_indices ``=` `np.random.choice(number_of_rows, ``                                  ``size``=``1``, ``                                  ``replace``=``False``)`` ` `# display random rows``print``(``"\nRandom row:"``)``row ``=` `data[random_indices, :]``print``(row)`

Output:

My Personal Notes arrow_drop_up