[問題] pandas 迴圈運算

作者: allenlenten (...)   2017-06-22 16:28:52
大家好 我想要問怎麼改變FOR 迴圈的起始和終值
比如說 我現有一個DF讀進來,然後
acc = df[df['成交時間']>=90500]
for mo in acc.index:
if df.iloc[mo,8] > 200:
buy = df.iloc[more+1,4]
out = 0
for mo in accumulate.index:
while out < -200:
out = out + df.iloc[mo+1,'vol']
我想要判斷df.iloc[mo,8] > 200 後買進 然後之前的加總的值小於200賣出
我以為mo 可以連貫的.好像不是這樣 要如何只做一次這個index就好
我要怎麼控制這個迴圈的起始值呢? 或是有更好的解法 謝謝
作者: Scinfaxi (Scinfaxi)   2017-06-22 20:53:00
acc的index是0開始的依序整數就可以吧?如果不是是否改成for mo in range(len(acc.index))?
作者: Django (Cython)   2017-06-22 21:30:00
是說acc = df[df['成交時間']>=90500] <--有這種語法@@?
作者: allenlenten (...)   2017-06-22 21:55:00
這是我取得條件的index。他不是從0開始 有時候index也不是斗連續 可能有跳號
作者: ntumath (math mad)   2017-06-23 04:44:00
iloc的用法就是把需要格子的座標當作參數,所以你要先取得你要的index名字的座標。用看看get_loc吧
作者: HenryLiKing (HenryLiKing)   2017-06-23 08:52:00
回gjango 有喔 中間那段會回傳一堆T和F 然後是T的就會被抓到喔
作者: Django (Cython)   2017-06-23 09:58:00
嗯~~~df的型態是什麼啊 我本來以為是dict
作者: max533 (向陽花)   2017-06-24 20:22:00
回樓上,df應該是pandas的物件Dataframe

Links booklink

Contact Us: admin [ a t ] ucptt.com