dimanche 6 mai 2018

Iterating if statements of columns through rows of Python dataframe

I am trying to obtain a final 'process' array by filtering the raw 'hitdt' array for '-1'; the column that contains -1 in a particular row in hitdt would determine the values of that row in 'process'. I am using if statements in what I suspect a very laborious way, but could not find working methods.

Currently, running def frame() does not return any errors, but when I check the resulting 'process' array, new columns remain NaN. 'hitdt' is the input data frame with 4 column series (hitdt['t1'] to hitdt['t4']). 'process' is an empty output dataframe. previous appending of data into hitdt series columns not involving 'if' statements were fine.

Is there a way to determine which column of a certain row in a data frame == a value, then apply statements to that row only, and iterate through all rows?

def frame():
    global hitdt, process
    #v1 
    for i, row in hitdt.iterrows():
        if -1 == i in hitdt['t3']:
            process['tau1'] = hitdt['t2']-hitdt['t1']
            process['tau2'] = hitdt['t4']-hitdt['t1']
            process['xx'] = geom['x2']
            process['yy'] = geom['y2']
            process['rho1'] = sqrt(square(geom['x2']-geom['x1']) + square(geom['y2']-geom['y1']))
            process['alpha'] = 2.357067
        elif -1 == i in hitdt['t4']:
            process['tau1'] = hitdt['t3']-hitdt['t2']
            process['tau2'] = hitdt['t1']-hitdt['t2']
            process['xx'] = geom['x3']
            process['yy'] = geom['y3']
            process['rho1'] = sqrt(square(x3-x2) + square(y3-y2))
            process['alpha'] = 0.749619
        elif -1 == i in hitdt['t1']:
            process['tau1'] = hitdt['t4']-hitdt['t3']
            process['tau2'] = hitdt['t2']-hitdt['t3']
            process['xx'] = geom['x4']
            process['yy'] = geom['y4']
            process['rho1'] = sqrt(square(x3-x4) + square(y3-y4))
            process['alpha'] = -0.800233
        elif -1 == i in hitdt['t2']:
            process['tau1'] = hitdt['t1']-hitdt['t4']
            process['tau2'] = hitdt['t3']-hitdt['t4']
            process['xx'] = geom['x1']
            process['yy'] = geom['y1']
            process['rho1'] = sqrt(square(geom['x1']-geom['x4']) + square(geom['y1']-geom['y4']))
            process['alpha'] = -1.906772

...

[In]: process   
[Out]: 
jd      frac tau1 tau2 rho1   xx   yy alpha  hits
0     2457754  0.501143  NaN  NaN  NaN  NaN  NaN   NaN     3
1     2457754  0.508732  NaN  NaN  NaN  NaN  NaN   NaN     3
2     2457754  0.512411  NaN  NaN  NaN  NaN  NaN   NaN     3
3     2457754  0.513932  NaN  NaN  NaN  NaN  NaN   NaN     3

Aucun commentaire:

Enregistrer un commentaire