通过Python数据帧的行迭代if列的语句

时间:2018-05-06 08:01:29

标签: python loops if-statement dataframe series

我试图通过将原始'hitdt'数组过滤为'-1'来获得最终的'进程'数组;在hitdt的特定行中包含-1的列将确定'process'中该行的值。我正在使用我怀疑非常费力的陈述,但找不到工作方法。

目前,运行def frame()不会返回任何错误,但当我检查生成的“进程”数组时,新列仍为NaN。 'hitdt'是具有4列系列的输入数据帧(hitdt ['t1']到hitdt ['t4'])。 'process'是一个空输出数据帧。以前将数据附加到不涉及'if'语句的hitdt系列列中都没问题。

有没有办法确定数据框中某一行的哪一列==一个值,然后只将语句应用于该行,并遍历所有行?

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

1 个答案:

答案 0 :(得分:0)

我只会解决tau1专栏,因为其他专栏只是重复的事情。

您当前的代码是:

for i, row in hitdt.iterrows():
    if -1 == i in hitdt['t3']:
        process['tau1'] = hitdt['t2']-hitdt['t1']
    elif -1 == i in hitdt['t4']:
        process['tau1'] = hitdt['t3']-hitdt['t2']
    elif -1 == i in hitdt['t1']:
        process['tau1'] = hitdt['t4']-hitdt['t3']
    elif -1 == i in hitdt['t2']:
        process['tau1'] = hitdt['t1']-hitdt['t4']

我会这样做:

if_t3 = hitdt['t2']-hitdt['t1']
if_t4 = hitdt['t3']-hitdt['t2']
if_t1 = hitdt['t4']-hitdt['t3']
if_t2 = hitdt['t1']-hitdt['t4']

condlist = [hitdt.t3 == -1, hitdt.t4 == -1, hitdt.t1 == -1, hitdt.t2 == -1]
default = np.nan
tau1 = [if_t3, if_t4, if_t1, if_t2]
process['tau1'] = np.select(condlist, tau1, default)