简体   繁体   中英

Iterating each row with remaining rows in pandas data frame

I am trying to iterate each row in dataframe with subsequent row. The first iteration works but I want to iterate for all other iterations like [111,.....] with remaining and continues. How can I achieve it using iterator?

test = [[1,2,3,4,5,6,7,8,9,10],[11,22,33,44,55,66,77,88,99,100],[111,222,333,444,555,666,777,888,999,1000],[1111,2222,3333,4444,5555,6666,7777,8888,9999,10000]]
df=pd.DataFrame(test)
row_iterator = df.iterrows()
_, main_row = next(row_iterator)
for i, row in row_iterator:
    print("---------Main Row-------------------")
    print(main_row)
    print("----------------------------")
    print("-----------Row-----------------")
    print(row)
    print("----------------------------")
    print("------------i----------------")
    print(i)
    print("----------------------------")

You don't need to use next these cases, a for loop "knows" how to deal with iterators. Skip the next , just use the iterator directly:

for i, row in df.iterrows():
    print(i, row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM