I get the Weather forecast for my City with an API and write this data to a mysql database. For this i use panda and a dataframe. This Dataframe has 40 entries: 8 a day for the next 5 days. Every 3 hours, there is a new entry generated from the website. My program repeats itself every minute.
#get data
data = urllib.request.urlopen("http://api.openweathermap.org/data/2.5/forecast?id=2867714&APPID=fddaaa343ef234b26418093023a6d2a8")
data = json.loads(data.read())
df=json_normalize(data['list'])
df['city']=data['city']['name']
#select data
df = df[['dt_txt','main.temp','main.pressure','main.humidity','city']]
#write to mysql database
engine = create_engine('mysql+mysqlconnector://root:XXXXXXXX@localhost/munich_temp')
df.to_sql(name='sheet', con=engine, if_exists = 'replace', index=False)
This code replaces the table every time.
Is there a way to check if the last timestamp of my dataframe already exists? If it doesn't exist i want to add one entry to the mysql table and overwrite the previous 39 entries. In the end it should generate an infinite database with Weather forecasts and no duplicates.