我试图每隔2分钟运行代码,并试图通过while循环在特定的时间间隔访问df单元格值。但是不能访问相同的,即使在fn中声明df为全局。如何获得值df['col']。Iloc [-1] after while condition。感谢在这方面的帮助。提前谢谢。
import datetime
import pandas as pd
from time import sleep
import time, pause
from datetime import datetime
import schedule
from apscheduler.schedulers.blocking import BlockingScheduler
from schedule import every, repeat, run_pending
sched = BlockingScheduler(timezone='Asia/Kolkata')
@sched.scheduled_job('cron', day_of_week='mon-sat', hour=16, minute=12,second=0)
def scheduled_job():
print('job started....',datetime.now().time())
@repeat(schedule.every(2).minutes.at(":00"))
def ttm():
print('function check....')
def dta_frame():
global df
print("*** main loop ***", datetime.now().time())
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],'Age':[27, 24, 22, 32],'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],'Rank':[2, 4, 10, 1]}
df = pd.DataFrame(data)
print (df)
def addn():
x = 25*25
print(x, datetime.now().time())
def multi():
y= 10*50
print(y, datetime.now().time())
while True:
schedule.run_pending()
ts = time.time()
tgt= (datetime.fromtimestamp(ts + 118 - ts % 120))
d_time =(datetime.fromtimestamp(ts))
while d_time > tgt:
if df['Rank'].iloc[-1]==1:
addn()
time.sleep(1)
pause.until(tgt)
break
elif df['Rank'].iloc[-1]==0:
multi()
time.sleep(1)
pause.until(tgt)
break
else:
print("No loop executed",datetime.now().time())
time.sleep(1)
pause.until(tgt)
break
sched.start()
如果您想在dta_frame()
之外使用df
,请在dta_frame()
之外声明它或从子程序返回它。