用pandas函数代替下列语句:for i in range(len(data2_0_time3)): if (np.isnan(data2_0_time3.iat[i,0])==True): if 3<=i<=len(data2_0_time3)-4: 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 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 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-09 09:03:41 浏览: 76
Read data from the Excel spreadsheet.zip_Spreadsheet::Read_excel
可以使用以下代码代替:
# 判断data2_0_time3中哪些值为 NaN
nan_values = pd.isna(data2_0_time3.iloc[:, 0])
# 获取 3<=i<=len(data2_0_time3)-4 的索引
index_range = np.logical_and(np.arange(len(data2_0_time3)) >= 3, np.arange(len(data2_0_time3)) <= len(data2_0_time3)-4)
# 将符合要求的索引对应的数据设置为 0
data2_0_time3.loc[nan_values & index_range, ['speed_391_24', 'speed_391_23', 'speed_391_22']] = 0
# 遍历时获取前三个非 NaN 的值,并计算其平均值
for i in np.where(nan_values & index_range)[0]:
# 获取前三个非 NaN 的值
data = data2_0_time3.iloc[i-3:i, :].dropna()
# 计算平均值
if not data.empty:
data2_0_time3.loc[i, ['speed_391_24', 'speed_391_23', 'speed_391_22']] = data.mean().values
阅读全文