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) out_dw['低温赔付'] = out_dw.apply(lambda x:get_money_dw(x['diff']),axis=1) out_dw.to_csv(str(staCode)+'低温具体赔付详情.csv',encoding='gbk')
时间: 2023-06-15 19:07:30 浏览: 80
这段代码是用来计算每年的最低温度和前后两天的温度差,然后根据温度差来确定起始日期,并计算相应的低温赔付金额。具体来说,代码通过循环遍历每一年的数据,对于每一天的最低温度,计算它与前后两天最低温度的差值,取这三个差值的最大值作为该天的温度变化值(也就是diff)。然后根据所有天中diff的最大值,确定起始日期和对应的低温赔付金额。最后将结果保存到CSV文件中。
相关问题
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代码
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 列,可以添加该行代码
for i_tfbh in tf_list_all: iyear = int(str(i_tfbh)[0:4]) print(i_tfbh,iyear) wka_tf = tf_data[tf_data['tfbh']==i_tfbh].copy() wka_tf['distance'] = wka_tf.apply(lambda x:distance(x['longitude'],x['latitude'],lon_,lat_),axis=1) wka_tf['距离范围'] = pd.cut(wka_tf['distance'],bins=[0,80,999999],labels=['[0,80]','(80,Inf]'],right=True) wka_tf = wka_tf[wka_tf['distance']<=80].copy() if wka_tf.empty: continue wka_tf['风速范围'] = pd.cut(wka_tf['speed'],bins=[0,32.7,37,41.5,46.2,51,56.1,999999],\ labels=['[0,32.7)','[32.7,37)','[37,41.5)','[41.5,46.2)','[46.2,51)','[51,56.1)','[56.1,Inf)'],\ right=False) wka_tf = pd.merge(wka_tf,peifu,how='left',on=['风速范围','距离范围']) money = wka_tf['money'].max() out_tf = out_tf.append({'台风编号':i_tfbh,'年':iyear,'台风赔付':money},ignore_index=True)
这段代码看起来是用来处理一个包含台风信息的数据集,并计算每个台风对应的赔付金额。具体来说,它会遍历所有的台风编号,在数据集中找到对应的台风记录,并计算该记录与给定经纬度的距离。然后,它会基于距离将记录分为两个范围:[0,80]和(80,Inf),并过滤掉距离大于80的记录。接着,它会将速度分为七个范围,并基于速度范围和距离范围,从另一个数据集中查找对应的赔付金额。最后,它会将该台风的最大赔付金额和台风编号存入另一个数据集中。
阅读全文