- matplotlib - pandas - lxml

Pandas operations Demo using PyScript

Demo developed by Dr. Tirthajyoti sarkar

Check out the announcement from Anaconda about PyScript framework

Test: Random integers

PyScript loading and initializing...

import matplotlib.pyplot as plt import numpy as np integers = np.random.randint(0,100,100) fig1,ax1 = plt.subplots(1,1,dpi=150,figsize=(5,2)) ax1.hist(integers,bins=20,edgecolor='k',color='blue') ax1.set_title("Histogram of random integers",fontsize=11) pyscript.write('test-plot',fig1)

Pandas - random dataframe

Let's generate a Pandas DataFrame with some random nunbers (drawn from the ubiquitous Gaussian Normal distribution).


Interested in the statistics of this dataset?



Download file and analyze

Now, we will download a CSV file from an externally hosted URL (a GitHub repo) and show its contents and statistics.

First 5 records of the downloaded file


Statistics of the downloaded file


import pandas as pd, numpy as np from js import document from pyodide.http import open_url def random_table(n_cols=5,n_rows=20,*ags, **kws): cols = ['Col-'+str(i) for i in range(1,n_cols+1)] idx = ['Row-'+str(i) for i in range(1,n_rows+1)] data = np.round(np.random.normal(size=(n_rows,n_cols)),2) df = pd.DataFrame(data,columns=cols,index=idx) return df def show_table(*ags, **kws): out = Element('pandas-output') random_df = random_table() out.write(random_df.to_html(classes='pd-table')) def show_stats(*ags, **kws): out = Element('pandas-ops') input_div = document.getElementById("pandas-output") html_data = input_div.children[0].outerHTML df = pd.read_html(html_data,flavor='lxml')[0] stats_df = df.describe() out.write(stats_df.to_html(classes='pd-table')) def download_analyze(*ags, **kws): url=document.getElementById('file-url').value try: df = pd.read_csv(open_url(url)) pyscript.write('url-file-head',df.head(5).to_html(classes='pd-table')) pyscript.write('url-file-ops',df.describe().to_html(classes='pd-table')) except: pyscript.write('url-file-head',"Could not access the file")