在Python datafram的行中迭代列的if语句

2024-10-04 03:17:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过筛选'-1'的原始'hitdt'数组来获得最终的'process'数组;hitdt中特定行中包含-1的列将确定'process'中该行的值。我使用if语句的方式很费劲,但找不到工作方法。在

当前,运行def frame()不会返回任何错误,但是当我检查结果“process”数组时,新列仍然是NaN。 'hitdt'是4列系列的输入数据帧(hitdt['t1']到hitdt['t4'])进程'是空的输出数据帧。以前将数据追加到hidt系列列中,不涉及“if”语句是可以的。在

有没有一种方法可以确定数据框中某一行的哪一列==一个值,然后只对该行应用语句,然后遍历所有行?在

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

。。。在

^{pr2}$

Tags: 数据inprocesst1t3xxsquaregeom
1条回答
网友
1楼 · 发布于 2024-10-04 03:17:52

我只解决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']

我会这样做:

^{pr2}$

相关问题 更多 >