Python

Python is an interpreted high-level general-purpose programming language. Python’s design philosophy emphasizes code readability with its notable use of significant indentation.

Pretty Tables with PANDAS

Been teaching myself about using color-coded tables in PANDAS and exporting them to HTML and JPG as social media graphics. For example, this table show the 7-day rolling average of COVID positive tests, with low values highlighted green, and high values highlighted red. The background colors make the table much easier to read and interprete.

County Albany Greene Saratoga Schoharie Schenectady Rensselaer Washington
Test Date              
Jul 25 2.3 4.5 3.3 2.3 2.8 1.8 1.4
Jul 26 2.5 5.2 3.8 2.3 2.8 1.9 1.7
Jul 27 2.7 5.6 4.0 2.0 3.0 2.3 2.1
Jul 28 2.8 5.6 4.5 3.1 3.4 2.5 2.3
Jul 29 3.0 4.3 4.6 3.8 3.2 3.1 2.3
Jul 30 3.3 3.7 4.6 4.2 3.5 3.2 2.1
Jul 31 3.3 3.8 4.5 5.3 3.9 3.5 2.6
Aug 01 3.6 2.8 4.7 4.9 4.2 3.7 2.6
Aug 02 4.1 2.1 4.6 4.6 4.7 4.3 3.5
Aug 03 4.4 2.1 4.4 4.8 4.6 4.1 3.3
Aug 04 4.4 2.2 4.4 4.4 4.5 4.1 3.4
Aug 05 4.5 3.1 4.4 5.1 4.8 4.0 3.7
Aug 06 4.8 3.7 4.4 5.4 4.9 4.4 3.8
Aug 07 4.9 3.5 4.7 5.2 5.1 4.5 3.9
Aug 08 5.3 4.2 4.7 6.5 5.3 4.2 4.0
Aug 09 5.1 4.4 4.7 8.0 5.7 4.1 3.9
Aug 10 5.1 4.4 4.6 8.1 5.7 4.3 4.1
Aug 11 4.9 4.8 4.3 8.1 5.8 4.4 4.4
Aug 12 4.8 5.0 4.7 7.7 5.9 4.4 4.1
Aug 13 4.7 4.6 4.6 7.9 5.9 4.4 4.3
Aug 14 4.9 5.0 4.5 7.1 5.9 4.4 4.0
Aug 15 4.6 5.1 4.4 6.5 6.6 4.8 4.1
Aug 16 4.6 5.8 4.3 5.5 6.2 4.7 4.2
Aug 17 4.6 5.1 4.3 4.9 6.2 4.9 4.6
Aug 18 4.6 4.9 4.5 4.8 6.6 5.0 4.5
Aug 19 4.5 4.1 4.2 4.1 6.3 4.7 4.2
Aug 20 4.3 4.5 4.4 3.6 6.0 4.8 4.1
Aug 21 4.1 5.0 4.4 3.9 5.6 4.7 4.0
Aug 22 4.1 4.5 4.5 3.3 5.1 4.7 3.8
Aug 23 4.2 4.6 5.0 3.7 5.0 4.5 3.4

The code to make this table is quite simple to, as is most things in PANDAS once your learn the basic codes.

import pandas as pd
df=pd.read_csv("https://health.data.ny.gov/api/views/xdss-u53e/rows.csv?accessType=DOWNLOAD", parse_dates=['Test Date'])

# create pivot table
tr=df.pivot(index='Test Date', columns='County', values='Rolling Positive Rate').tail(30)[['Albany','Greene','Saratoga','Schoharie','Schenectady','Rensselaer','Washington']]

# format index text
tr.index = tr.index.strftime("%b %d")

# red-green palette
import seaborn as sns
cm = sns.diverging_palette(150, 10, s=80, l=55, n=9, as_cmap=True)

# display in jupyter
tr.style.background_gradient(cmap=cm)

# export to HTML table (shown above)
with open('/tmp/covid-weekly.html', 'w') as f:
    f.write(tr.style.background_gradient(cmap=cm).render())

# alternatively, export as an HTML image using dataframe_image library (pip install dataframe_image)
import dataframe_image as dfi
dfi.export(tr.style.background_gradient(cmap=cm), '/tmp/covid-alt.table.png')