Skip to content

Instantly share code, notes, and snippets.

View mkhorasani's full-sized avatar

Mohammad Khorasani mkhorasani

View GitHub Profile
df = pd.read_csv('C:/Users/..../data.csv')
for i in range(0,len(df)):
while len(str(df.iloc[i][1]).replace('.0','')) < 4:
df.iloc[i,1] = '0' + str(df.iloc[i][1]).replace('.0','')
temp = datetime.datetime.strptime(str(df.iloc[i][0]).replace('.0','') + str(df.iloc[i][1]).replace('.0',''),'%Y%m%d%H%M')
df.iloc[i,0] = temp.strftime('%B')
df.iloc[i,1] = temp.strftime('%I%p')
df = df.groupby(['Month','Hour'],sort=False,as_index=False).mean().round(1)
month_hours = {'January': {'12AM': None, '01AM': None, '02AM': None, '03AM': None, '04AM': None, '05AM': None,
'06AM': None, '07AM': None, '08AM': None, '09AM': None, '10AM': None, '11AM': None,
'12PM': None, '01PM': None, '02PM': None, '03PM': None, '04PM': None, '05PM': None,
'06PM': None, '07PM': None, '08PM': None, '09PM': None, '10PM': None, '11PM': None},
....
'December': {'12AM': None, '01AM': None, '02AM': None, '03AM': None, '04AM': None, '05AM': None,
'06AM': None, '07AM': None, '08AM': None, '09AM': None, '10AM': None, '11AM': None,
'12PM': None, '01PM': None, '02PM': None, '03PM': None, '04PM': None, '05PM': None,
for i in range(len(df)):
month_hours[df.iloc[i][0]][df.iloc[i][1]] = df.loc[i]['Value']
data_rows = list(month_hours.values())
data = []
for i in range(0,len(data_rows)):
data.append(list(data_rows[i].values()))
fig = px.imshow(data,
labels=dict(x="Hour", y="Month", color="Value"),
x=['12AM','01AM','02AM','03AM','04AM','05AM',
'06AM','07AM','08AM','09AM','10AM','11AM',
'12PM','01PM','02PM','03PM','04PM','05PM',
'06PM','07PM','08PM','09PM','10PM','11PM'],
y=['January','February','March','April','May','June',
'July','August','September','October','November','December']
)
st.title('Month vs. Hour Heatmap')
st.title('Dataframe')
st.dataframe(df)
st.markdown(download_csv('Heatmap Dataframe',pd.DataFrame(df)),unsafe_allow_html=True)
def download_csv(name,df):
csv = df.to_csv()
base = base64.b64encode(csv.encode()).decode()
file = (f'<a href="data:file/csv;base64,{base}" download="%s.csv">Download file</a>' % (name))
return file
from sqlalchemy import create_engine
import psycopg2
import pandas as pd
import streamlit as st
engine = create_engine("postgresql://<username>:<password>@localhost:5432/records_db")
engine_dataset = create_engine("postgresql://<username>:<password>@localhost:5432/datasets_db")
engine.execute("CREATE TABLE IF NOT EXISTS records (name text PRIMARY KEY, details text[])")
def write_record(name,details,engine):
engine.execute("INSERT INTO records (name,details) VALUES ('%s','%s')" % (name,details))
def read_record(field,name,engine):
result = engine.execute("SELECT %s FROM records WHERE name = '%s'" % (field,name))
return result.first()[0]
def update_record(field,name,new_value,engine):
engine.execute("UPDATE records SET %s = '%s' WHERE name = '%s'" % (field,new_value,name))