Home > Enterprise >  I want to stop a loop while the condition are true and by using .loc
I want to stop a loop while the condition are true and by using .loc

Time:06-01

I want to stop while the condition are satisfied. There is the code :

for i in range(0,70):
    table_mix_non_rest.loc[(table_mix_non_rest['e_apd_M_' str(i)]>=100) & ((table_mix_non_rest['s_dpd_M_' str(i)]>=90)), 'ENTRY_OBSERVATION_DATE'] = table_mix_non_rest['r_date_reporting_M_' str(i)]

and here in image : [1]: https://i.stack.imgur.com/F5xx3.png

CodePudding user response:

I would suggest that if you are iterating through pandas rows that you try to use the iterrows() instead of how you're doing it. By implementing your your loop using iterrows() you'd be able to test out whatever condition you want on the row from that column and put a simple if statement to break when you want to get out of it.

import pandas as pd

df = pd.DataFrame({'c1': [10, 11, 13, 18], 'c2': [100, 110, 120, 140]})
df = df.reset_index()  # make sure indexes pair with number of rows
for index, row in df.iterrows():
    print(row['c1'], row['c2'])
    if row['c1'] == 13:
        break # or do whatever you want

CodePudding user response:

Sorry, but I am a bit confused. You want to stop the code at each iteration step? It's a bit unclear what you are trying to achieve

  • Related