[英]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'; 我试图通过将原始的“ hitdt”数组过滤为“ -1”来获得最终的“ process”数组; the column that contains -1 in a particular row in hitdt would determine the values of that row in 'process'.
在hitdt的特定行中包含-1的列将确定'process'中该行的值。 I am using if statements in what I suspect a very laborious way, but could not find working methods.
我正在以怀疑的方式使用if语句,但是找不到工作方法。
Currently, running def frame()
does not return any errors, but when I check the resulting 'process' array, new columns remain NaN
. 当前,运行
def frame()
不会返回任何错误,但是当我检查生成的'process'数组时,新列仍为NaN
。 'hitdt' is the input data frame with 4 column series (hitdt['t1'] to hitdt['t4']). 'hitdt'是具有4列序列的输入数据帧(hitdt ['t1']至hitdt ['t4'])。 'process' is an empty output dataframe.
'process'是一个空的输出数据框。 previous appending of data into hitdt series columns not involving 'if' statements were fine.
以前将数据附加到不涉及'if'语句的hitdt系列列中是可以的。
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
I'm only going to solve the tau1
column, because the others are just repeated cases of the same thing. 我将只解决
tau1
列,因为其他只是同一事物的重复情况。
Your current code is: 您当前的代码是:
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']
I'd do this: 我会这样做:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.