用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
时间: 2023-06-08 14:06:41 浏览: 79
可以使用下列代码来代替原有的语句:
```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 参数。
相关问题
用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
可以使用以下代码替代:
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,我们需要进行判断。
用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
可以这样使用 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 函数替换原代码中的赋值语句即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)