I have a data frame that fetches data from SQL and sends out an email based on the condition of a column. But these columns are not present in SQL, I need to create them after creating the data frame. The issue here is I have a while loop where it checks for the condition of the column every 10 sec. I noticed that the while loop works perfectly with all the conditions but the data frame is not refreshed from SQL since it is outside the while loop. If I put the data frame inside the while loop, the last_email_sent
is initialized as None
is affected and gives the wrong output. Below is my pseudo code where I have the logic described.
#initialisation and fetching of the table from SQL
df = pd.read_sql('SELECT * FROM stations', cnxn)
df['Timeflag'] = now - df['last_reported']
df['last_email_sent'] = None
while True:
for index in df.index:
if(df['Timeflag'] == 1 and df.loc[df.index[index], "Last_email_sent"] is None:
print('pass')
minutes = divmod((now - df.loc[index, "Last_email_sent"]).total_seconds(), 60)[0]
elif df.loc[index, 'Time flag'] == 1 and minutes < min:
print('fail')
minutes = divmod((now - df.loc[index, "Last_email_sent"]).total_seconds(), 60)[0]
else:
print('false')
time.sleep(10)
The issue is that I cannot do something like below as inside the for loop the last_email_sent
cannot be None
and has to retain the last updated value that was prevalent after 1st iteration of while loop.
while True:
#initialisation and fetching of the table from SQL
df = pd.read_sql('SELECT * FROM stations', cnxn)
df['Timeflag'] = now - df['last_reported']
df['last_email_sent'] = None
Is there any other method to call the data frame inside for loop and thereby calculating other columns simultaneously?