mardi 31 mars 2020

Speed up IF/ELIF search over index in multiple lists?

Is there a faster way to run the following code: (checks if a value in the df.index is in multiple lists, and if so it appends a string to one list and the index to another list)

truth = []
t_ind = []
for ind in df.index.values:
    if ind in t4:
        truth.append('a')
        t_ind.append(ind)
    elif ind in t8:
        truth.append('b')
        t_ind.append(ind)
    elif ind in nk:
        truth.append('c')
        t_ind.append(ind)
    elif ind in mono:
        truth.append('d')
        t_ind.append(ind)
    elif ind in b:
        truth.append('e')
        t_ind.append(ind)
    else:
        truth.append('Other')
        t_ind.append(ind)

where t4, t8, mono, nk and b are seperate lists with index values (int)

Aucun commentaire:

Enregistrer un commentaire