mercredi 7 octobre 2020

Numpy / Pandas: randomly choose n items from a list, for where n is a random integer, for each index

I am new with numpy and pandas, and I am trying to write this piece of code which creates a pandas Series. For every index in the Series I want to randomly choose a random number of interests, 1 - 3 in this case with no duplicates, from the list above. I would like to find ways to improve my code if possible.

Thanks

def random_interests(num):
    interests = [1, 2, 3, 4, 5, 6]
    stu_interests = []
    for n in range(num):
        stu_interests.append(np.random.choice(interests, np.random.randint(1, 4), replace=False))
    rand_interests = pd.Series(stu_interests)



Aucun commentaire:

Enregistrer un commentaire