mercredi 21 novembre 2018

Return a header with a conditional statement on a dataframe

My data:

Header
Case | PD | PP | 50 | 80 | FW | BA | IA | EA | NA


Case1 | 00 | 23 | 00 | 00 | 21 | 00 | 00 | 00  | 00

Case2 | 00 | 23 | 00 | 00 | 12 | 32 | 14 | 00  | 00


What I want: For every case, I want to return the last header. For example: Case1 would be FW, because FW is the last header with a value (21).

For Case2 it would be "IA", because it is the last header with a value (14).

Right now I have the following:

def getStatus(dataframe):
    for index, row in dataframe.iterrows():

    def lookup(phase, name):
        if phase is not '00':
            return name

    x = lookup(row['FW'], "FW")
    x = lookup(row['BA'], "BA")
    x = lookup(row['IA'], "IA")
    x = lookup(row['EA'], "EA")
    x = lookup(row['NA'], "NA")
    x = lookup(row['PP'], "PP")

From a logical view, I thought something like x will do several lookups (def lookup) and always overwrite the value above, if existent.

What I am doing wrong here, what would be a good approach? My approach is not well designed I guess, but I also think a big function with a lot of if and elif is also bad.

Aucun commentaire:

Enregistrer un commentaire