[問題] for loop後將list存入dataframe

看板Python作者 (深呼吸~)時間6年前 (2019/05/18 20:02), 編輯推噓2(203)
留言5則, 3人參與, 6年前最新討論串1/1
各位版大好,以下程式中有3個for迴圈,目的勢將i, j, k的值寫入一dataframe df2, 不過運算結果一直出現"list index out of range"的錯誤訊息 不知道有沒有什麼方式可以解決? 又或者有其他方式可以達到預期的結果? 再麻煩各位大大解惑,謝謝! 預期達到的結果如下: [[0, 50, 125, 100, 200, 300], [0, 50, 250, 100, 200, 300], [50, 100, 125, 100, 200, 300], [50, 100, 250, 100, 200, 300]] 程式內容如下: percentile_gap = 50 before_gap = 125 behind_gap = 100 before_max = before_gap*2 df2 = pd.DataFrame({'Lbound':[], 'Ubound':[], 'before_X_days':[]}) for i in range(0, 100-percentile_gap+1, percentile_gap): correlation = [] correlation.append ( i ) correlation.append ( i + percentile_gap ) for j in range(125, before_max+1, before_gap): correlation.append ( j ) for k in range(100, 300+1, behind_gap): df2['+'+str(k)+' days'] = df2['Lbound'] correlation.append ( k ) df2.iloc[len(df2)] = [i][j][correlation] -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.125.195.36 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1558180977.A.82F.html

05/18 21:04, 6年前 , 1F
pandas不用這樣運作啦
05/18 21:04, 1F

05/18 21:05, 6年前 , 2F
你存出3個list,直接pd.DataFrame操作就可以
05/18 21:05, 2F

05/20 02:55, 6年前 , 3F
最後一個for loop不知道在剪什麼
05/20 02:55, 3F

05/27 05:36, 6年前 , 4F
100,301,100??
05/27 05:36, 4F

05/27 05:36, 6年前 , 5F
明顯是range問題吧
05/27 05:36, 5F
文章代碼(AID): #1St_HnWl (Python)
文章代碼(AID): #1St_HnWl (Python)