mardi 15 septembre 2020

Define Counter for ID´s in Database

I have a database that look approximately from the structure like this only with a few million´s of rows:

enter image description here

Now I have written a Counter that always count when the the column vg (speed) starts at 0 and end at 0

def chop_phasen(df):

df['phasen'] = 0

#need to define some monitor variables
phasen_counter = 0
during_process = False
current_id = None 

current_id = df['ID'].values[0]

for id_fahrt, index, vg in zip(df['ID'].values , df.index, df.vg.values):

    #new id
    if(id_fahrt!=current_id):

        current_id = id_fahrt
        phasen_counter = 0

    #starting of a process
    if(vg!=0.0) and (during_process == False):

        phasen_counter+=1
        during_process=True

        df.at[index, 'phasen'] = phasen_counter

    if(vg!= 0.0) and (during_process==True):

        df.at[index, 'phasen'] = phasen_counter

    if(vg==0.0) and (during_process==True):

        during_process=False

return df

The Problem that I have found now is, that the Database doesn't always start at vg = 0 or end at vg = 0 when a new ID start. how can I tell the counter to start with the ID and end with the ID for the counting? It still should count the phases that are in a ID. Phases are not counted for vg = 0

Aucun commentaire:

Enregistrer un commentaire