用pandas函数代替下列语句:data2_0_time3.loc[i,'speed_391_24']=0 data2_0_time3.loc[i,'speed_391_23']=0 data2_0_time3.loc[i,'speed_391_22']=0 k=0 r=i for j in range(1,r+1): if np.isnan(data2_0_time3.iat[i-j,0])==False: data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.iat[i,0]+data2_0_time3.iat[i-j,0] data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.iat[i,1]+data2_0_time3.iat[i-j,1] data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.iat[i,2]+data2_0_time3.iat[i-j,2] k=k+1 for j in range(1,4): if np.isnan(data2_0_time3.iat[i+j,0])==False: data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.iat[i,0]+data2_0_time3.iat[i+j,0] data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.iat[i,1]+data2_0_time3.iat[i+j,1] data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.iat[i,2]+data2_0_time3.iat[i+j,2] k=k+1 data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.iat[i,0]/k data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.iat[i,1]/k data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.iat[i,2]/k
时间: 2023-06-08 12:06:15 浏览: 63
eq_data_30_day_m1.rar
可以使用以下代码替代:
for j in range(1,r+1):
if not np.isnan(data2_0_time3.iloc[i-j,0]):
data2_0_time3.loc[i,'speed_391_24']+=data2_0_time3.iloc[i-j,0]
data2_0_time3.loc[i,'speed_391_23']+=data2_0_time3.iloc[i-j,1]
data2_0_time3.loc[i,'speed_391_22']+=data2_0_time3.iloc[i-j,2]
k+=1
if k>0:
data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.loc[i,'speed_391_24']/k
data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.loc[i,'speed_391_23']/k
data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.loc[i,'speed_391_22']/k
这段代码使用了 for 循环和 iloc 函数来遍历数据,并计算出 speed_391_24、speed_391_23 和 speed_391_22 的平均值。注意,当 k=0 时,不能除以 0,我们需要进行判断。
阅读全文