Change table outputs to be tabulate based

Now using markdown-interpreted (through ipython) output of tabulate
package from compute cells for tables.

Removes issues around latex output for dataframes and leads to more
coherent output.
This commit is contained in:
Marty Oehme 2022-09-08 13:51:13 +02:00
parent ced71b6bac
commit 2aa11bc793
Signed by: Marty
GPG key ID: B7538B8F50A1C800
6 changed files with 26 additions and 5 deletions

View file

@ -267,7 +267,7 @@ crosstab = pd.crosstab(aid_grouped['Year'], aid_grouped['Sector'], margins=True,
# Rename and reorder columns # Rename and reorder columns
crosstab = crosstab[['Trade development', 'Business growth', "Women's rights support", 'Food security', 'All']] crosstab = crosstab[['Trade development', 'Business growth', "Women's rights support", 'Food security', 'All']]
crosstab.fillna('0.00') Markdown(tabulate(crosstab.fillna("0.00"), headers="keys", tablefmt="github", floatfmt=".2f"))
``` ```
::: {custom-style="caption"} ::: {custom-style="caption"}

View file

@ -279,7 +279,7 @@ crosstab = pd.crosstab(wateraid_grouped['Year'], wateraid_grouped['Sector'], mar
crosstab.columns = ['Basic water supply', 'Education and training', 'Large water supply', 'All'] crosstab.columns = ['Basic water supply', 'Education and training', 'Large water supply', 'All']
crosstab = crosstab[['Basic water supply', 'Large water supply', 'Education and training', 'All']] crosstab = crosstab[['Basic water supply', 'Large water supply', 'Education and training', 'All']]
crosstab Markdown(tabulate(crosstab.fillna("0.00"), headers="keys", tablefmt="github", floatfmt=".2f"))
``` ```
::: {custom-style="caption"} ::: {custom-style="caption"}

View file

@ -314,8 +314,7 @@ crosstab = pd.crosstab(floodaid_grouped['Year'], floodaid_grouped['Sector'], mar
crosstab.columns = ['Basic water supply', 'Disaster risk reduction', 'Large water supply', 'All'] crosstab.columns = ['Basic water supply', 'Disaster risk reduction', 'Large water supply', 'All']
crosstab = crosstab[['Basic water supply', 'Large water supply', 'Disaster risk reduction', 'All']] crosstab = crosstab[['Basic water supply', 'Large water supply', 'Disaster risk reduction', 'All']]
#crosstab.style.format(escape="latex") Markdown(tabulate(crosstab.fillna("0.00"), headers="keys", tablefmt="github", floatfmt=".2f"))
crosstab
``` ```
::: {custom-style="caption"} ::: {custom-style="caption"}

View file

@ -35,6 +35,11 @@ uga = df.loc[df['c3'] == "UGA"]
vnm = df.loc[df['c3'] == "VNM"] vnm = df.loc[df['c3'] == "VNM"]
``` ```
```{python}
from IPython.display import Markdown
from tabulate import tabulate
```
```{python} ```{python}
# Set up the data extraction and figure drawing functions # Set up the data extraction and figure drawing functions
import plotly.express as px import plotly.express as px

18
poetry.lock generated
View file

@ -1028,6 +1028,17 @@ build = ["cython (>=0.29.26)"]
develop = ["cython (>=0.29.26)"] develop = ["cython (>=0.29.26)"]
docs = ["sphinx", "nbconvert", "jupyter-client", "ipykernel", "matplotlib", "nbformat", "numpydoc", "pandas-datareader"] docs = ["sphinx", "nbconvert", "jupyter-client", "ipykernel", "matplotlib", "nbformat", "numpydoc", "pandas-datareader"]
[[package]]
name = "tabulate"
version = "0.8.10"
description = "Pretty-print tabular data"
category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
[package.extras]
widechars = ["wcwidth"]
[[package]] [[package]]
name = "tenacity" name = "tenacity"
version = "8.0.1" version = "8.0.1"
@ -1124,7 +1135,7 @@ python-versions = ">=3.7"
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.9" python-versions = "^3.9"
content-hash = "dcd0e115c0c55bebb2a464655fe91c749ef16ed875ebfdf0d3b04f8ee1bb8ccc" content-hash = "9a8889c9ff9cf08f3529c276b3d41da95fcfdc871cacea6ab96d55ec3e8d3e0a"
[metadata.files] [metadata.files]
appnope = [ appnope = [
@ -2003,6 +2014,11 @@ statsmodels = [
{file = "statsmodels-0.13.2-cp39-cp39-win_amd64.whl", hash = "sha256:39daab5a8a9332c8ea83d6464d065080c9ba65f236daf6a64aa18f64ef776fad"}, {file = "statsmodels-0.13.2-cp39-cp39-win_amd64.whl", hash = "sha256:39daab5a8a9332c8ea83d6464d065080c9ba65f236daf6a64aa18f64ef776fad"},
{file = "statsmodels-0.13.2.tar.gz", hash = "sha256:77dc292c9939c036a476f1770f9d08976b05437daa229928da73231147cde7d4"}, {file = "statsmodels-0.13.2.tar.gz", hash = "sha256:77dc292c9939c036a476f1770f9d08976b05437daa229928da73231147cde7d4"},
] ]
tabulate = [
{file = "tabulate-0.8.10-py3-none-any.whl", hash = "sha256:0ba055423dbaa164b9e456abe7920c5e8ed33fcc16f6d1b2f2d152c8e1e8b4fc"},
{file = "tabulate-0.8.10-py3.8.egg", hash = "sha256:436f1c768b424654fce8597290d2764def1eea6a77cfa5c33be00b1bc0f4f63d"},
{file = "tabulate-0.8.10.tar.gz", hash = "sha256:6c57f3f3dd7ac2782770155f3adb2db0b1a269637e42f27599925e64b114f519"},
]
tenacity = [ tenacity = [
{file = "tenacity-8.0.1-py3-none-any.whl", hash = "sha256:f78f4ea81b0fabc06728c11dc2a8c01277bfc5181b321a4770471902e3eb844a"}, {file = "tenacity-8.0.1-py3-none-any.whl", hash = "sha256:f78f4ea81b0fabc06728c11dc2a8c01277bfc5181b321a4770471902e3eb844a"},
{file = "tenacity-8.0.1.tar.gz", hash = "sha256:43242a20e3e73291a28bcbcacfd6e000b02d3857a9a9fff56b297a27afdc932f"}, {file = "tenacity-8.0.1.tar.gz", hash = "sha256:43242a20e3e73291a28bcbcacfd6e000b02d3857a9a9fff56b297a27afdc932f"},

View file

@ -12,6 +12,7 @@ openpyxl = "^3.0.10"
matplotlib = "^3.5.3" matplotlib = "^3.5.3"
plotly-express = "^0.4.1" plotly-express = "^0.4.1"
kaleido = "^0.2.1,!=0.2.1.post1" kaleido = "^0.2.1,!=0.2.1.post1"
tabulate = "^0.8.10"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]