从特定的行索引遍历熊猫数据框

时间:2019-08-07 22:18:41

标签: python pandas

我可以使用iterrows函数在pandas数据帧上进行迭代,但是我想知道如何对给定索引的行进行迭代?

我知道一个人可以做类似的事情:

index_to_start = 100
current = 0
for _, row in frame.iterrows():
    if current < index_to_start:
        continue
    # Do something 

但是,这看起来有点丑陋,我想知道是否有更清晰,更直接的方法来做到这一点?

1 个答案:

答案 0 :(得分:3)

您不需要其他if来控制

index_to_start = 100
for _, row in frame.iloc[index_to_start:,:].iterrows():
    #do something

此外,在大熊猫中,我们通常不会旅行。

您可以进行np.where

np.where(df.reset_index().index<100, 'nothing', ' do someting')