简体   繁体   中英

Python: Combine for loop in while loop and iterate rows in a data

I would like to iterate rows of data and print the final row after reaching a condition. My code looks like this

D_sum = 4.39e-05
count = 0
while D_sum < 1:
   for row in df.iterrows():
       count += 1
       D_sum += D_sum
       print('count', count)
       print('D_sum', D_sum)
       print('row', row)
       break  # When i write break the loop is executing correct but, its not printing final row. Its printing first row of data. 

If I don't write Break, the loop is continuing and ending after certain iterations.

I tried to write this code

count = 0
flag = True
while flag:
    for row in df.iterrows():
        count += 1
        D_sum += (df[['Di']].sum()).values
        print('count:', count)
        print('D_sum', D_sum)
        print(row)
if D_sum == 1:
   flag = False

But this also continuing until certain iterations (Infinite loop) Please help me correcting the code in python

D_sum = 4.39e-05
count = 0
for row in df.iterrows():
    count += 1
    D_sum += D_sum
    print('count', count)
    print('D_sum', D_sum)
    # Check your condition here 
    if D_sum >= 1:
       print('row', row)
       break 

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