用pandas函数代替该函数:def NaND(): data=data2[['speed_391_24','speed_391_23','speed_391_22']] for i in x_index: data.loc[i,'speed_391_24']=np.nan data.loc[i,'speed_391_23']=np.nan data.loc[i,'speed_391_22']=np.nan return data
时间: 2023-06-09 22:05:15 浏览: 174
你好,我是ChitGPT,理解了你的要求,我的回答是使用pandas的fillna函数可以代替NaND函数,代码如下:
def NaND():
data=data2[['speed_391_24','speed_391_23','speed_391_22']]
for i in x_index:
data.loc[i,['speed_391_24','speed_391_23','speed_391_22']]=np.nan
return data.fillna(value=np.nan)
这样做可以更快捷地将数据中的指定值替换为NaN。希望我的回答能够帮到你。
相关问题
用pandas函数代替下列语句:def NaND(): data=data2[['speed_391_24','speed_391_23','speed_391_22']] for i in x_index: data.loc[i,'speed_391_24']=np.nan data.loc[i,'speed_391_23']=np.nan data.loc[i,'speed_391_22']=np.nan return datadata2_0=NaND() nan_index=[] for i in range(len(data2_0)): if (np.isnan(data2_0.iat[i,0])==True): nan_index.append(i)
你可以使用pandas的fillna函数来实现这个功能,具体的代码如下所示:
data = data2[['speed_391_24','speed_391_23','speed_391_22']]
data = data.fillna(np.nan)
data2_0 = data
nan_index = []
for i in range(len(data2_0)):
if (np.isnan(data2_0.iat[i,0])==True):
nan_index.append(i)
希望能够帮到你!
用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
data2_0_time3[data2_0_time3.iloc[:, 0].isna()] = data2_0_time3[data2_0_time3.iloc[:,0].isna()].apply(lambda x: \
pd.Series([0 if 3 <= i <= len(data2_0_time3)-4 \
else np.nan if np.isnan(x.iloc[i,0]) \
else (x.iloc[i,0]+x.iloc[i-1,0]+x.iloc[i-2,0])/3 \
for i in range(len(x))]), axis=1)
阅读全文