Python循环一整天,当当前时间等于给定时间时,做一些事情

时间:2016-12-14 08:52:52

标签: python pandas

我有一个Pandas DataFrame df,它有三列(timefromto)。我想执行一个for循环df['time']的函数。当前时间等于df['time']中的时间时,调用另一个函数,比如打印一些东西。每行只执行一次。在实际数据中,脚本将在云中24小时执行。

import pandas as pd
df=pd.DataFrame({'time':['08:35','09:35','09:45','10:10'],
                 'from':['SHH','SZH','WXH','ZJH'],
                 'to':['NJH','NJH','NJH','NJH']})
df

    from    time    to
0   SHH     08:35   NJH
1   SZH     09:35   NJH
2   WXH     09:45   NJH
3   ZJH     10:10   NJH

例如,当前时间为08:35时,请打印Time is reached, train from SHH to NJH,当前时间为09:35时,请打印Time is reached, train from SZH to NJH。我不知道如何修改下面的代码来满足我的需求 工作。需要帮助。

import datetime
import time

def ex(a,b):
    print("Time is reached. train from {} to {}".format(a, b))

time_ls = list(df['time'])
from_ls = list(df['from'])
to_ls = list(df['to'])

def run():
    for i in range(len(df['time'])):
        while time.strftime("%H:%M", time.localtime()) == df['time'][i]:
            time_ls.remove(df['time'][i])
            yield ex(from_ls[i],to_ls[i])

1 个答案:

答案 0 :(得分:1)

如果要从满足特定条件的pandas.DataFrame中提取行,则需要对DataFrame进行切片,而不是手动迭代其所有行并自行检查该条件。 pandas实现此方法比任何手动尝试都要快。

一旦获得的数据帧只包含与当前时间(小时和分钟)匹配的行,您就可以遍历那个较小的DataFrame并打印每行的结果(因为你知道它只包含匹配的那些行。

请参阅以下示例:

from datetime import datetime as dt
import pandas as pd

if __name__ == '__main__':
    df = pd.DataFrame({
        'time': ['08:35','09:35','09:45','10:10'],
        'from': ['SHH','SZH','WXH','ZJH'],
        'to':   ['NJH','NJH','NJH','NJH']})

    ct = dt.strftime(dt.now(), '%H:%M') # Get current hours and minutes
    dn = df.loc[df['time'] == ct] # Slice DataFrame based on 'time' column

    for row in dn.iterrows():
        # Iterate over all rows that meet the condition and print it
        print('{time:s} -- train from {from:s} to {to:s}.'.format(**dict(row[1])))

它会产生(如果此示例中ct字符串匹配08:35的小时和分钟):

08:35 -- train from SHH to NJH.