簡體   English   中英

使用itertools()在Python 3中一次遍歷3行

[英]Iterate through 3 rows at once in Python 3 using itertools()

這段代碼有助於一次迭代2行。 我們如何在熊貓的數據框中一次遍歷3行?

例如

1,2,3

2,3,4

3,4,5等

from itertools import tee
from itertools import zip_longest as izip
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

for (idx1, row1), (idx2, row2) in pairwise(stock_fut.iterrows()):
    print(idx1,row1,"\n\n",idx2,row2,"\n\n")

此代碼有效

from itertools import tee
from itertools import zip_longest as izip
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b, c = tee(iterable,3)
    next(b);next(c);next(c)
    return izip(a, b, c)

for (idx1, row1), (idx2, row2), (idx3, row3) in pairwise(stock_fut.iterrows()):
    print(idx1,row1,"\n\n",idx2,row2,"\n\n",idx3, row3,"\n\n")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM