data = data[(data['mon_day']>=1210) | (data['mon_day']<=430)].copy() data['Year_new'] = np.where(data['mon_day']<=430,data['Year']-1,data['Year']) out_dw = pd.DataFrame(columns={'年'}) for iyear in range(styr,edyr+1): wka = data[data['Year_new']==iyear].copy() diff_max = 0 stdate = 0 for i in range(0,len(wka)-2): t_1 = wka.iloc[i]['TEM_Min'] t_2 = wka.iloc[i+1]['TEM_Min'] t_3 = wka.iloc[i+2]['TEM_Min'] diff = max(t_1-t_2,t_1-t_3,t_2-t_3) if diff > diff_max: diff_max = diff stdate = wka.iloc[i]['date'] out_dw = out_dw.append({'年':wka.iloc[0]['Year_new'],'stdate':stdate,'diff':diff_max},ignore_index=True)转成R代码
时间: 2023-06-15 14:07:07 浏览: 77
data <- data[(data$mon_day>=1210) | (data$mon_day<=430),]
data$Year_new <- ifelse(data$mon_day<=430, data$Year-1, data$Year)
out_dw <- data.frame(matrix(ncol = 2, nrow = 0))
colnames(out_dw) <- c("年", "stdate")
for (iyear in styr:edyr) {
wka <- data[data$Year_new == iyear,]
diff_max <- 0
stdate <- 0
for (i in 1:(nrow(wka)-2)) {
t_1 <- wka[i, "TEM_Min"]
t_2 <- wka[i+1, "TEM_Min"]
t_3 <- wka[i+2, "TEM_Min"]
diff <- max(t_1-t_2, t_1-t_3, t_2-t_3)
if (diff > diff_max) {
diff_max <- diff
stdate <- wka[i, "date"]
}
}
out_dw <- rbind(out_dw, c(iyear, stdate))
}
colnames(out_dw) <- c("年", "stdate", "diff") # 如果需要 diff 列,可以添加该行代码
阅读全文