用pandas的rolling函数替代下列语句: 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 22:08:02 浏览: 75
可以这样使用 Pandas 的 rolling 函数替代原代码:
data2_0_time3.loc[i, ['speed_391_24', 'speed_391_23', 'speed_391_22']] = 0
if not np.isnan(data2_0_time3.iat[i, 0]):
rolling_data = data2_0_time3[max(i-3, 0):i][['speed_391_24', 'speed_391_23', 'speed_391_22']]
rolling_mean = rolling_data.mean()
data2_0_time3.loc[i, ['speed_391_24', 'speed_391_23', 'speed_391_22']] = rolling_mean
这里的 rolling_data 是一个 DataFrame,包含最近 3 行的 'speed_391_24', 'speed_391_23', 'speed_391_22' 这三列数据。rolling_mean 是这三列数据的平均值,即新的 'speed_391_24', 'speed_391_23', 'speed_391_22' 的值。最后,用 loc 函数替换原代码中的赋值语句即可。
相关问题
用pandas的rolling函数代替下列语句: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
可以使用以下代码替换上述语句:
data2_0_time3.loc[i, ['speed_391_24', 'speed_391_23', 'speed_391_22']] = np.nan_to_num(data2_0_time3.iloc[max(i-3,0):i+1, [0,1,2]].rolling(window=4, min_periods=1).sum().iloc[-1]/min(i+1, 4))
用pandas的rolling函数代替下列语句: 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=len(data2_0_time3)-1-i 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 for j in range(1,r): 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
可以使用下列代码来代替原有的语句:
```Python
cols = ['speed_391_24', 'speed_391_23', 'speed_391_22']
rolling_df = data2_0_time3.loc[:i][cols].rolling(window=3, min_periods=1)
data2_0_time3.loc[i, cols] = rolling_df.mean().iloc[-1].values
```
这里用到了 rolling() 函数来实现对DataFrame的滚动计算,计算方式采用的是 rolling_df.mean(),然后用 iloc 从中选择最后一行的值进行填充。值得注意的是,这里的 rolling() 函数默认是窗口为3,但是要达到原有语句的效果,还需要加上 min_periods=1 参数。
阅读全文