用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 10:06:24 浏览: 84
可以用如下语句代替:
```
col_names = ['speed_391_24', 'speed_391_23', 'speed_391_22']
nan_index = data2_0_time3.index.isna().to_numpy().nonzero()[0]
for j in range(1, len(nan_index)):
temp_index = slice(max(0, nan_index[j-1]), nan_index[j])
temp_data = data2_0_time3.loc[temp_index, col_names].copy()
temp_data.dropna(inplace=True)
k = len(temp_data)
if k > 0:
temp_mean = temp_data.mean()
data2_0_time3.loc[temp_index, col_names] = pd.concat([temp_mean]*k).values.reshape((k, 3))
last_index = max(0, nan_index[-1])
temp_data = data2_0_time3.iloc[last_index:last_index+3, :3].copy()
temp_data.dropna(inplace=True)
k = len(temp_data)
if k > 0:
temp_mean = temp_data.mean()
data2_0_time3.iloc[last_index:last_index+3, :3] = pd.concat([temp_mean]*k).values.reshape((k, 3))
```
阅读全文