看板 Python 關於我們 聯絡資訊
大家好 1. 我現在有一組程式碼有問題 upperb=NDVI_LST[col[0]]>0.0 lowerb=NDVI_LST[col[0]]<0.1 edge_1=NDVI_LST[(upperb & lowerb)] lst_values=[] for i in edge_1[col[0]]: LST=NDVI_LST.loc[NDVI_LST[col[0]]==i,col[1]] NDVI=i for j in LST.values: lst_values.append(j) df.loc[0.0,'w1月']=max(lst_values) lst_values=[] for i in edge_1[col[0]]: LST=NDVI_LST.loc[NDVI_LST[col[0]]==i,col[1]] NDVI=i for j in LST.values: lst_values.append(j) df.loc[0.0,'d1月']=min(lst_values) 因為我現在會需要有12個月的重複計算以及 upperb以及lowerb的重複計算(0.0 ,0.1, 0.2, 0.3.....1.0) 原本我是用下面的code跑 可是每次都會剩下最後一個值然後把前面的值蓋掉 NDVI_bound=np.around(np.linspace(0,1,11).tolist(),decimals=2) for k in range(1,13): for j in range(23): for i in range(10): upperb=NDVI_LST[col[int(j)]]>NDVI_bound[int(i)] lowerb=NDVI_LST[col[int(j)]]<NDVI_bound[int(i)+1] edge_1=NDVI_LST[(upperb & lowerb)] lst_values=[] for l in edge_1[col[int(j)]]: LST=NDVI_LST.loc[NDVI_LST[col[int(j)]]==l,col[int(j)+1]] NDVI=i for m in LST.values: lst_values.append(m) if lst_values==[]: df.loc[NDVI_bound[int(i)+1],'w'+str(k)+'月']=0 df.loc[NDVI_bound[int(i)+1],'d'+str(k)+'月']=0 else: df.loc[NDVI_bound[int(i)+1],'w'+str(k)+'月']=max(lst_values) df.loc[NDVI_bound[int(i)+1],'d'+str(k)+'月']=min(lst_values) 我想問一下大家 這樣要怎麼縮減這個程式碼才會是對的? 不然我光是用一個一個貼 就要2000多行程式碼 可是明明是重複做一樣的動作... 2.查表 我找的值是從上面那張表出來的 D1 W1 0.1 23.7 22.4 0.2 25.8 26.88 0.3 29.8 30.64 . . . 1.0 35.26 38.31 我現在給他一個值 他要怎麼找到在這張表的哪個範圍? 比如說 我現在給他27.33這個數值 他要怎麼找到是在0.2~0.3之間 然後跟我說上限是29.8 下限是25.8 這樣? 有兩個問題 再麻煩各位了QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.134.51.64 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1618821116.A.0F5.html
TitanEric: 第一個感覺if else那區塊要縮進去2個tab 04/19 17:44
TitanEric: 不然都會讀到最後一個i 04/19 17:45
TitanEric: 第二個可以參考interval Index 04/19 17:51
aassdd926: 聽起來需要寫成function 04/19 22:16