我有一个数据框,可从SQL提取数据并根据列的条件发送电子邮件。但是这些列在SQL中不存在,我需要在创建数据框后创建它们。这里的问题是我有一个while循环,它每10秒检查一次列的状况。我注意到,while循环在所有条件下均能完美运行,但是由于SQL不在while循环之外,因此无法从SQL刷新数据帧。如果我将数据帧放入while循环内,则last_email_sent
会被初始化,因为None
会受到影响并提供错误的输出。下面是我的伪代码,其中描述了逻辑。
伪代码:
#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)
仅当在while循环的每次迭代中获取数据帧时,数据帧才会刷新。因此,您应该在df
循环内而不是外部创建while
实例。
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
# Rest of your code follows here...