简体   繁体   English

用 pandas 数据帧中的剩余行迭代每一行

[英]Iterating each row with remaining rows in pandas data frame

I am trying to iterate each row in dataframe with subsequent row.我正在尝试用后续行迭代 dataframe 中的每一行。 The first iteration works but I want to iterate for all other iterations like [111,.....] with remaining and continues.第一次迭代有效,但我想迭代所有其他迭代,如 [111,.....] 剩余并继续。 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.你不需要在这些情况下使用next一个,for 循环“知道”如何处理迭代器。 Skip the next , just use the iterator directly:跳过next ,直接使用迭代器即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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