python - 在 While 循环内刷新 Python 中的数据帧

标签 python sql python-3.x pyodbc

我有一个数据框,它从 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 并且必须保留 1st 之后流行的最后更新值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

是否有其他方法可以在 for 循环中调用数据框,从而同时计算其他列?

最佳答案

如果我以正确的方式理解了你的问题,你可以执行以下操作,但请注意,它还没有准备好使用代码,我只是想演示逻辑

First_Start = True  # first time we define db colum to None
Last_Email_Sent = None  # when we sent the last email

while True:

    # read data from db heare and do what you need
    df = pd.read_sql('SELECT * FROM stations', cnxn)
    df['Timeflag'] = now - df['last_reported']
    if First_Start:
        df['last_email_sent'] = None
        First_Start = False  # never again will be True
    else:
        df['last_email_sent'] = Last_Email_Sent

    while True:
       for index in df.index:  # cheack all you want in df
           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')

        Last_Email_Sent  = ??? # define new value here!
        break # all work is done and you go out of the while loop
    time.sleep(10)

    # now you can apply to db again to get a new df

希望答案对您有用。

关于python - 在 While 循环内刷新 Python 中的数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58373399/

相关文章:

python - Pyspark - 将 rfc 2822 列转换为 tymstamp 列

python - 如何在元组的元组中获取最大元素?

mysql - 关于数据库更新/插入速率限制的一些查询(基于 SQL 或基于 NoSQL)

sql - 自定义处理以在 SQL 中将行转换为列

python - 如何在python3中json.dumps字节对象

python - 在 AWS S3 中分块创建大型 zip 文件

python - libao 示例在编译为 python 模块时不起作用

python - 如何使用 Selenium WebDriver python 访问隐藏的文件上传字段

python - 我如何通过我的规范文件说服 PyInstaller 在它生成的 EXE 中包含 libvlc.dll?

php - 如何限制此 MySQL 查询的结果?