mercredi 28 octobre 2020

Use of numpy.select when conditions and hence choices are too many

I have just started using numpy.select() given specific conditions and choices list.

Currently my code looks like this:

conditions = [
    data['column_name'].values == mylist[0],
    data['column_name'].values == mylist[1],
    data['column_name'].values == mylist[2],
    data['column_name'].values == mylist[3],
    data['column_name'].values == mylist[4],
    data['column_name'].values == mylist[5],
    data['column_name'].values == mylist[6]
] 

    choices = [
    data['column_name'].multiply(data['xyz'])*mylist[0]*Price/another_list[0],
    data['column_name'].multiply(data['xyz'])*mylist[1]*Price/another_list[1],
    data['column_name'].multiply(data['xyz'])*mylist[2]*Price/another_list[2],
    data['column_name'].multiply(data['xyz'])*mylist[3]*Price/another_list[3],
    data['column_name'].multiply(data['xyz'])*mylist[4]*Price/another_list[4],
    data['column_name'].multiply(data['xyz'])*mylist[5]*Price/another_list[5],
    data['column_name'].multiply(data['xyz'])*mylist[6]*Price/another_list[6]
]

outcome = np.select(conditions, choices)

What I am trying to find out, is what if I had say 30 conditions and 30 different choices? Do I have to fill conditions and choices lists manually? Is there any shortcut similar to the one below?

conditions = [data['column_name'].values == mylist]

choices = [data['column_name'].multiply(data['xyz'])*mylist*Price/another_list]

outcome = np.select(conditions, choices)

I hope I passed the message, looking forward to your ideas. Thank you

Aucun commentaire:

Enregistrer un commentaire