在Python数据帧的行中迭代列的if语句
我试图通过过滤'-1'的原始'hitdt'数组来获得最终的'process'数组;hitdt中特定行中包含-1的列将确定“process”中该行的值。我使用if语句的方式非常费劲,但找不到工作方法 当前,运行在Python数据帧的行中迭代列的if语句,python,loops,if-statement,dataframe,series,Python,Loops,If Statement,Dataframe,Series,我试图通过过滤'-1'的原始'hitdt'数组来获得最终的'process'数组;hitdt中特定行中包含-1的列将确定“process”中该行的值。我使用if语句的方式非常费劲,但找不到工作方法 当前,运行def frame()不会返回任何错误,但当我检查结果“process”数组时,新列仍保留NaN。 “hitdt”是具有4列系列的输入数据帧(hitdt['t1']到hitdt['t4'])进程“”是空的输出数据帧。以前将数据追加到hitdt系列列中,不涉及“if”语句是可以的 有没有办法确
def frame()
不会返回任何错误,但当我检查结果“process”数组时,新列仍保留NaN
。
“hitdt”是具有4列系列的输入数据帧(hitdt['t1']到hitdt['t4'])进程“”是空的输出数据帧。以前将数据追加到hitdt系列列中,不涉及“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
我只想解决
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)
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)