用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 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-08 20:06:33 浏览: 85
可以使用pandas的rolling函数来代替循环,代码如下:
data2_0_time3.loc[i,['speed_391_24','speed_391_23','speed_391_22']] = [0,0,0]
if i >= 3:
rollingdata = data2_0_time3.iloc[i-3:i,:3]
rollingdata = rollingdata.dropna()
rollingcnt = len(rollingdata)
if rollingcnt > 0:
data2_0_time3.loc[i,['speed_391_24','speed_391_23','speed_391_22']] = rollingdata.mean()
其中,rolling函数会在数据窗口中滑动,计算滑动窗口内的均值。如果数据窗口中包含NaN值,则在均值计算中除去该值。最终,使用mean函数计算每个窗口的均值,并在原始数据中更新对应的值。
相关问题
用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
可以使用以下代码来代替原来的语句:
```
data2_0_time3.loc[i,['speed_391_24','speed_391_23','speed_391_22']]=[0,0,0]
for j in range(1,r+1):
if not np.isnan(data2_0_time3.iat[i-j,0]):
data2_0_time3.loc[i,['speed_391_24','speed_391_23','speed_391_22']]=[
data2_0_time3.iat[i,0]+data2_0_time3.iat[i-j,0],
data2_0_time3.iat[i,1]+data2_0_time3.iat[i-j,1],
data2_0_time3.iat[i,2]+data2_0_time3.iat[i-j,2]
]
else:
break
for j in range(1,4):
if not np.isnan(data2_0_time3.iat[i+j,0]):
data2_0_time3.loc[i,['speed_391_24','speed_391_23','speed_391_22']]=[
data2_0_time3.iat[i,0]+data2_0_time3.iat[i+j,0],
data2_0_time3.iat[i,1]+data2_0_time3.iat[i+j,1],
data2_0_time3.iat[i,2]+data2_0_time3.iat[i+j,2]
]
else:
break
k = j-1 if j>1 else 1
data2_0_time3.loc[i,['speed_391_24','speed_391_23','speed_391_22']]=[
data2_0_time3.iat[i,0]/k,
data2_0_time3.iat[i,1]/k,
data2_0_time3.iat[i,2]/k
]
```
这个代码将先将 'speed_391_24'、'speed_391_23'、'speed_391_22' 这三列设置为0,然后通过两个循环计算这三列的值,并最终将这三列的值除以 k 得到平均值。
用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))
阅读全文