我有一个数据框,它从 SQL 中获取数据并根据列的条件发送一封电子邮件。但是这些列在 SQL 中不存在,我需要在创建数据框后创建它们。这里的问题是我有一个 while 循环,它每 10 秒检查一次列的条件。我注意到 while 循环在所有条件下都能完美运行,但数据框没有从 SQL 刷新,因为它在 while 循环之外。如果我将数据框放在 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)
问题是我不能在 for 循环中执行类似下面的操作,last_email_sent
不能None
并且必须保留在第一次循环循环后流行的最后更新值。
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
是否有任何其他方法可以在 for 循环中调用数据框,从而同时计算其他列?