for循环中Python中刷新数据框的问题

问题描述 投票:0回答:1

我有一个数据框,可从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)
python sql dataframe while-loop refresh
1个回答
0
投票

仅当在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...
© www.soinside.com 2019 - 2024. All rights reserved.