Vi har i dette faget jobbet med JavaScript. Dette er et programmeringsspråk som fungerer bra for å innhente og presentere reelle datasett. Men når man skal analysere data så er ikke JavaScript et spesielt godt verktøy. Da er det nyttig å bruke et verktøy som heter Pandas. Dette verktøyet er laget for Python. Vi skal derfor gjøre noe morsomt. Vi skal bruke noe som heter PyScript. Dette er Python i nettleseren. Hvordandette gjøres er besrkvet i psdf dokumentet pandas.pdf som du finner under filer idette repositoriet. I dette pdf dokumenteter det flere kodesnutter. Siden disse ikke kan lastes ned er disse kodesnuttene lagt inn her og kan enkelt lastes ned. Sidetallene henviser til sidetallet i pdf dokumentet.
Hovedkoden er et HTML dokument (se s. 2). Vi legger Python koden vår inn i taggen som heter . Når vi bruker taggen vil Pythnon koden vises på nettsiden i en REPL (Read Evaluate Print Loop, ala det vi har i Jupyter Notebook). Dette kan vi endre ved på erstate taggen med en tagg.
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Pyscript</title><!-- Her leser vi inn pyscript biblioteket --><scriptdefersrc="https://pyscript.net/latest/pyscript.js"></script></head><style></style><body><!-- I <py-config> taggen installerer vi pandas og andre aktuelle bibliotek --><py-config> packages = ["pandas","matplotlib","jinja2","requests"]</py-config><!--Inne i <py-repl> taggen kan vi skrive vanlig Python kode.Når vi bruker repl så får vi vist Pyrthon koden i nettleseren<py-repl> kan erstatters med <py-script>. Da vises ikke python koden.--><py-repl>
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[{"navn":"Per","adresse":"skoleveien 5"},{"navn":"Ola","adresse":"skoleveien 6"}]df=pd.DataFrame(data)display(df)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[{"navn":"Per","adresse":"skoleveien 5"},{"navn":"Ola","adresse":"skoleveien 6"}]df=pd.DataFrame(data)display(df.loc[:,["navn"]])
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[{"navn":"Per","adresse":"skoleveien 5"},{"navn":"Ola","adresse":"skoleveien 6"}]df=pd.DataFrame(data)display(df.loc[[0],["navn"]])
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[{"navn":"Per","adresse":"skoleveien 5"},{"navn":"Ola","adresse":"skoleveien 6"}]df=pd.DataFrame(data)display(df.loc[[0,1],["navn"]])
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[{"navn":"Per","adresse":"skoleveien 5"},{"navn":"Ola","adresse":"skoleveien 6"}]df=pd.DataFrame(data)display(df.loc[[1,0],["navn"]])
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[ {"navn":"Per","adresse":"skoleveien 5","alder":;45,"hoyde":176,"vekt":79}, {"navn":"Ola","adresse":"skoleveien 6","alder":56,"hoyde":186,"vekt":89}, {"navn":"Hans","adresse":"skoleveien 8","alder":66,"hoyde":197,"vekt":99} ]df=pd.DataFrame(data)display(df)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=[ {"navn":"Per","adresse":"skoleveien 5","alder":;45,"hoyde":176,"vekt":79}, {"navn":"Ola","adresse":"skoleveien 6","alder":56,"hoyde":186,"vekt":89}, {"navn":"Hans","adresse":"skoleveien 8","alder":66,"hoyde":197,"vekt":99} ]df=pd.DataFrame(data)farge=['r','g','#0000FF']linje=['g','b','r']fig,ax=plt.subplots(figsize=(8,3),layout='constrained')foriirange (0,len(df.index)):ax.bar(df.loc[i,"navn"],df.loc[i,"vekt"],color=farge[i],edgecolor=linje[i],df.loc[i,"vekt"],width=0.2)ax.legend(bbox_to_anchor=(1,1))ax.set_title("Vekt for ulike personer")display(plt)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondf=pd.read_json(open_url("navn.json"))farge=['r','g','#0000FF']linje=['g','b','r']fig,ax=plt.subplots(figsize=(8,3),layout='constrained')foriirange (0,len(df.index)):ax.bar(df.loc[i,"navn"],df.loc[i,"vekt"],color=farge[i],edgecolor=linje[i],df.loc[i,"vekt"],width=0.2)ax.legend(bbox_to_anchor=(1,1))ax.set_title("Vekt for ulike personer")display(plt)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondf=pd.read_json(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))display(df)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondf=pd.read_json(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))display(df['values'])
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path=["values"])display(df)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path=["values"],meta=["station","zone"])df1=df[['fromTime','toTime','value','color','station']]display(df1)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondefrow_style(row):returnpd.Series('background-color:#'+row.color+';'+'color:blue;'+'font-size:20px;' ,row.index)data=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path='values')df1=df[['fromTime','toTime','value','color']]display(df1.style.apply(row_style,axis=1))
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondefrow_style(row):returnpd.Series('background-color:#'+row.color+';'+'color:blue;'+'font-size:20px;' ,row.index)data=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path='values')df1=df[['fromTime','toTime','value','color']]display(df1.style.apply(row_style,axis=1).hide(axis="columns",subset='color').hide(axis="index").set_table_styles( [ {'selector':'th,td','props':'border: 1px solid black;' } ] ).set_table_attributes('style="border-collapse:collapse"'))
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path='values')avstand=-0.3fig,ax=plt.subplots(figsize=(11,6),layout='constrained')foriinrange (0,len(df.index)):ax.bar(df.loc[i,"fromTime"],df.loc[i,"value"],color="#"+df.loc[i,"color"],width=0.5)disply(plt)
importpandasaspdimportjinja2importmatplotlib.pyplotaspltfrompyodide.httpimportopen_urlimportjsondata=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path='values')avstand=-0.3fig,ax=plt.subplots(figsize=(11,6),layout='constrained')foriinrange (0,len(df.index)):ax.bar(df.loc[i,"fromTime"],df.loc[i,"value"],color="#"+df.loc[i,"color"],width=0.5)ax.text(avstand,df.loc[i,"value"]+1,df.loc[i,"value"])avstand=avstand+1plt.xticks(rotation=90)plt.title('luftkvalitetsindeks')plt.ylabel('luftkvalitetsindeks (mikrogram/m^3')plt.xlabel('dato')disply(plt)
importpandasaspdimportjinja2importmatplotlib.pyployaspltfrompyodide.httpimportopen_urlimportjsondata=json.load(open_url("https://api.nilu.no/aq/historical/2022-02-02/2022-02-03/ids/3124"))df=pd.json_normalize(data,record_path='values')df2=df['index'].value_counts().div(len(df.index)).multiply(100)fig,ax=plt.subplots()ax.pie(df2,labels=['lav','moderat','høy'],autopct='%1.1f%%',colors=['#6ee86e','#ff9900','#ff0000'])display(plt)
importjsimportjinja2importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfrompyodide.httpimportopen_urldf=pd.read_json(open_url("./05.json"))s=df.start_station_name.value_counts()mest_pop=s.head(3)minst_pop=s.tail(3)s=pd.concat([mest_pop,minst_pop])fig,ax=plt.subplots()ax.bar(s.index,s,color=['r','g','b','y','c','m'])plt.title('Tre mest og minst populære startlokasjoner')plt.xlabel('startlokasjon')plt.ylabel('Antall (popularitet)')plt.xticks(rotation=30)plt.tight_layout()display(plt)
importjsimportjinja2importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfrompyodide.httpimportopen_urldf=pd.read_json(open_url("./05.json"))df2=df.groupby('end_station_name').mean(numeric_only="True").sort_values('duration',ascending=False).head(3)df2['min']=df2['duration'].div(60).round(0)fig,ax=plt.subplots()ax.bar(df2.index,df2['min'],color=['r','g','b'])plt.xlabel('sluttlokasjon')plt.ylabel('Minutter')plt.title('Sluttlokasjonene med høyeste gjennmsnittsvarighet')display(plt)
importjsimportjinja2importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfrompyodide.httpimportopen_urldf=pd.read_json(open_url("./05.json"))df['dato']=pd.to_datetime(df['started_at'])df['ukedag_nr']=df['dato'].dt.weekdays=df['ukedag_nr'].value_counts().sort_index()dager=['mandag','tirsdag','onsdag','torsdag','fredag','lørdag','søndag']fig,ax=plt.subplots()ax.bar(dager,s,color=['r','b','g','c','m','y','k'])plt.title('Antall turer totalt per dag')plt.ylabel('Antall turer')display(plt)
importjsimportjinja2importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfrompyodide.httpimportopen_urldf=pd.read_json(open_url("./05.json"))s=df.start_station_name.value_counts()mest_pop=s.head(3)minst_pop=s.tail(3)s=pd.concat([mest_pop,minst_pop])fig,ax=plt.subplots()ax.bar(s.index,s,color=['r','g','b','y','c','m'])forbarsinax.containers:ax.bar_label(bars)plt.title('Tre mest og minst populære startlokasjoner')plt.xlabel('startlokasjon')plt.ylabel('Antall (popularitet)')plt.xticks(rotation=30)plt.tight_layout()display(plt)
importjsimportjinja2importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfrompyodide.httpimportopen_urldf=pd.read_json(open_url("./05.json"))df2=df.groupby('end_station_name').mean().sort_values('duration',ascending=False).head(3)df2['min']=df2['duration'].div(60).round(0)fig,ax=plt.subplots()ax.bar(df2.index,df2['min'],color=['r','g','b'])forbarsinax.containers:ax.bar_label(bars)plt.xlabel('Sluttlokasjon')plt.ylabel('Minutter')plt.title('Sluttlokasjoner med høyeste gjennomsnittsvarighet')display(plt)
importjsimportjinja2importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfrompyodide.httpimportopen_urldf=pd.read_json(open_url("./05.json"))df['dato']=pd.to_datetime(df['started_at'])df['ukedag_nr']=df['dato'].dt.weekdays=df['ukedag_nr'].value_counts().sort_index()dager=['mandag','tirsdag','onsdag','torsdag','fredag','lørdag','søndag']fig,ax=plt.subplots()ax.bar(dager,s,color=['r','g','b','c','m','y','k'])forbarsinax.containers:ax.bar_label(bars)plt.title('Antall turer totalt per dag')plt.ylabel('Antall turer')display(plt)