mercredi 2 juin 2021

How to extract random rows from a numpy array within unique values of one column

I have a numpy array and want to extract some random rows of it. This solution does it but my case is a little bit detailed. I want to find some random rows based on the last column of my array. This column has some unique values and I want to extract randoms in each unique group. This is my array:

import numpy as np
my_array=np.array([['1.66', '1.67', 'group_B'],\
                   ['1.', '5.65', 'group_C'],\
                   ['9.06', '10.2', 'group_B'],\
                   ['2.', '0.2', 'group_B'],\
                   ['11.', '2.1', 'group_C']])

For example, I have two groups in last column of my data. From each group, I want to extract randomly n number of rows (for example 1 row per group). I do appreciate any help to do what I want in Python.




Aucun commentaire:

Enregistrer un commentaire