Python 如何引用行迭代器中的另一行?

Python 如何引用行迭代器中的另一行?,python,pandas,Python,Pandas,我有以下代码 row_iterator = temp.iterrows() for i, row in row_iterator: row['InterE'] = row['xs'] - (row['xs'] - row['InterS']) * exp(-row['ak1']) if row['InterE'][:-1] < 1: row['InterS'] = row['InterE'][:-1] else: row['Inter

我有以下代码

row_iterator = temp.iterrows()
for i, row in row_iterator:
    row['InterE'] = row['xs'] - (row['xs'] - row['InterS']) * exp(-row['ak1'])
    if row['InterE'][:-1] < 1:
        row['InterS'] = row['InterE'][:-1]
    else:
        row['InterS'] = row['InterE'][:-1] - row['InterE'][:-1] * row['xi'][:-1]

您能帮助我吗?

您应该避免迭代,尤其是当您可以对操作进行矢量化时

所以

#计算整个数据帧的“InterE”列
温度['InterE']=temp['xs']-(temp['xs']-temp['InterS'])*exp(-temp['ak1'])
#现在,对于小于1的值,请指定上一行的值,这就是shift所做的
临时位置[temp['InterE']<1,'InterS']=temp['InterE'].shift(-1)
#对于其他条件,执行替代计算并分配
临时位置[temp['InterE']>=1,'InterS']=temp['InterE'].shift(-1)-(temp['InterE'].shift(-1)*temp['xi'].shift(-1))
让我知道,如果这是你想要的,如果没有,然后张贴数据和所需的输出

invalid index to scalar variable.
# calculate 'InterE' column for entire dataframe
temp['InterE'] = temp['xs'] - (temp['xs'] - temp['InterS']) * exp(-temp['ak1'])
# now for those values less than 1 assign the previous row value, this is what shift does
temp.loc[temp['InterE'] < 1, 'InterS'] = temp['InterE'].shift(-1)
# for the other condition perform the alternative calculation and assign
temp.loc[temp['InterE'] >=1, 'InterS'] = temp['InterE'].shift(-1) - (temp['InterE'].shift(-1) * temp['xi'].shift(-1))