How to randomly select rows of an array in Python with NumPy ? Last Updated : 25 Feb, 2021 Comments Improve Suggest changes Like Article Like Report 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: Comment More infoAdvertise with us Next Article How to randomly select rows of an array in Python with NumPy ? A aankit71 Follow Improve Article Tags : Python Python-numpy Python numpy-program Python numpy-Random Practice Tags : python Similar Reads How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read How to Randomly Select rows from Pandas DataFrame In Pandas, it is possible to select rows randomly from a DataFrame with different methods. Randomly selecting rows can be useful for tasks like sampling, testing or data exploration.Creating Sample Pandas DataFrameFirst, we will create a sample Pandas DataFrame that we will use further in our articl 3 min read How to Select Random Rows from a Matrix in MATLAB? A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array.  Syntax:matrix_name(i,j)where, i is the row number, and  J is the column number which is to be indexed. Example 1: Matlab % MATLAB code for sel 2 min read How to Swap Two Rows in a NumPy Array One common task you might encounter when working with NumPy arrays is the need to swap two rows. Swapping rows can be essential in data preprocessing, reshaping data, or reordering data to perform specific analyses in Python. In this article, we will explore different methods to swap two rows in a N 4 min read How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem 2 min read Like