用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 15:06:20 浏览: 77
使用 Pandas 函数可以代替以上代码:
```python
cols = ['speed_391_24', 'speed_391_23', 'speed_391_22']
data2_0_time3[cols] = data2_0_time3[cols].fillna(0)
for j in range(1, r):
check = ~data2_0_time3.iloc[i-j].isna()
data2_0_time3.loc[i, cols[check]] = data2_0_time3.iloc[i, check] + data2_0_time3.iloc[i-j, check]
data2_0_time3[cols] /= k
```
相关问题
用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
可以使用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 得到平均值。
阅读全文