Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve output format #75

Open
tompollard opened this issue Nov 13, 2018 · 2 comments
Open

improve output format #75

tompollard opened this issue Nov 13, 2018 · 2 comments

Comments

@tompollard
Copy link
Owner

tompollard commented Nov 13, 2018

The current approach relies on pandas to output the table to tex, csv, etc. Add custom approach to improve the output quality.

Consider tabulate:

from tableone import TableOne
import pandas as pd
import matplotlib.pyplot as plt
import tabulate

url="https://raw.githubusercontent.com/tompollard/tableone/master/data/pn2012_demo.csv"
data=pd.read_csv(url)
overall_table = TableOne(data, label_suffix=True)

x = overall_table.tableone.reset_index()
t = tabulate.tabulate(x, tablefmt="grid", headers=['isnull', 'overall'],showindex=False)
print(t)
+-------------------+------+----------+--------------+
|                   |      | isnull   | overall      |
+===================+======+==========+==============+
| n                 |      |          | 1000         |
+-------------------+------+----------+--------------+
| Age, mean (SD)    |      | 0        | 65.0 (17.2)  |
+-------------------+------+----------+--------------+
| SysABP, mean (SD) |      | 291      | 114.3 (40.2) |
+-------------------+------+----------+--------------+
| Height, mean (SD) |      | 475      | 170.1 (22.1) |
+-------------------+------+----------+--------------+
| Weight, mean (SD) |      | 302      | 82.9 (23.8)  |
+-------------------+------+----------+--------------+
| ICU, n (%)        | CCU  | 0        | 162 (16.2)   |
+-------------------+------+----------+--------------+
| ICU, n (%)        | CSRU |          | 202 (20.2)   |
+-------------------+------+----------+--------------+
| ICU, n (%)        | MICU |          | 380 (38.0)   |
+-------------------+------+----------+--------------+
| ICU, n (%)        | SICU |          | 256 (25.6)   |
+-------------------+------+----------+--------------+
| MechVent, n (%)   | 0    | 0        | 540 (54.0)   |
+-------------------+------+----------+--------------+
| MechVent, n (%)   | 1    |          | 460 (46.0)   |
+-------------------+------+----------+--------------+
| LOS, mean (SD)    |      | 0        | 14.2 (14.2)  |
+-------------------+------+----------+--------------+
| death, n (%)      | 0    | 0        | 864 (86.4)   |
+-------------------+------+----------+--------------+
| death, n (%)      | 1    |          | 136 (13.6)   |
+-------------------+------+----------+--------------+
isdupe = x.duplicated(subset='variable')
x['variable'] = x['variable'].where(~isdupe, '')
t = tabulate.tabulate(x, tablefmt="grid", headers=['isnull', 'overall'],showindex=False)
print(t)
+-------------------+------+----------+--------------+
|                   |      | isnull   | overall      |
+===================+======+==========+==============+
| n                 |      |          | 1000         |
+-------------------+------+----------+--------------+
| Age, mean (SD)    |      | 0        | 65.0 (17.2)  |
+-------------------+------+----------+--------------+
| SysABP, mean (SD) |      | 291      | 114.3 (40.2) |
+-------------------+------+----------+--------------+
| Height, mean (SD) |      | 475      | 170.1 (22.1) |
+-------------------+------+----------+--------------+
| Weight, mean (SD) |      | 302      | 82.9 (23.8)  |
+-------------------+------+----------+--------------+
| ICU, n (%)        | CCU  | 0        | 162 (16.2)   |
+-------------------+------+----------+--------------+
|                   | CSRU |          | 202 (20.2)   |
+-------------------+------+----------+--------------+
|                   | MICU |          | 380 (38.0)   |
+-------------------+------+----------+--------------+
|                   | SICU |          | 256 (25.6)   |
+-------------------+------+----------+--------------+
| MechVent, n (%)   | 0    | 0        | 540 (54.0)   |
+-------------------+------+----------+--------------+
|                   | 1    |          | 460 (46.0)   |
+-------------------+------+----------+--------------+
| LOS, mean (SD)    |      | 0        | 14.2 (14.2)  |
+-------------------+------+----------+--------------+
| death, n (%)      | 0    | 0        | 864 (86.4)   |
+-------------------+------+----------+--------------+
|                   | 1    |          | 136 (13.6)   |
+-------------------+------+----------+--------------+
tompollard added a commit that referenced this issue Nov 18, 2019
@tompollard
Copy link
Owner Author

The tabulate method was added in 0.6.4:

# import libraries
from tableone import TableOne
import pandas as pd

# load sample data into a pandas dataframe
url="https://raw.githubusercontent.com/tompollard/tableone/master/data/pn2012_demo.csv"
data=pd.read_csv(url)

table = TableOne(data, label_suffix=True)
print(overall_table.tabulate(tablefmt = "fancygrid"))

outputs:

╒═══════════════════╤══════╤═══════════╤══════════════╕
│                   │      │ Missing   │ Overall      │
╞═══════════════════╪══════╪═══════════╪══════════════╡
│ n                 │      │           │ 1000         │
├───────────────────┼──────┼───────────┼──────────────┤
│ Age, mean (SD)    │      │ 0         │ 65.0 (17.2)  │
├───────────────────┼──────┼───────────┼──────────────┤
│ SysABP, mean (SD) │      │ 291       │ 114.3 (40.2) │
├───────────────────┼──────┼───────────┼──────────────┤
│ Height, mean (SD) │      │ 475       │ 170.1 (22.1) │
├───────────────────┼──────┼───────────┼──────────────┤
│ Weight, mean (SD) │      │ 302       │ 82.9 (23.8)  │
├───────────────────┼──────┼───────────┼──────────────┤
│ ICU, n (%)        │ CCU  │ 0         │ 162 (16.2)   │
├───────────────────┼──────┼───────────┼──────────────┤
│                   │ CSRU │           │ 202 (20.2)   │
├───────────────────┼──────┼───────────┼──────────────┤
│                   │ MICU │           │ 380 (38.0)   │
├───────────────────┼──────┼───────────┼──────────────┤
│                   │ SICU │           │ 256 (25.6)   │
├───────────────────┼──────┼───────────┼──────────────┤
│ MechVent, n (%)   │ 0    │ 0         │ 540 (54.0)   │
├───────────────────┼──────┼───────────┼──────────────┤
│                   │ 1    │           │ 460 (46.0)   │
├───────────────────┼──────┼───────────┼──────────────┤
│ LOS, mean (SD)    │      │ 0         │ 14.2 (14.2)  │
├───────────────────┼──────┼───────────┼──────────────┤
│ death, n (%)      │ 0    │ 0         │ 864 (86.4)   │
├───────────────────┼──────┼───────────┼──────────────┤
│                   │ 1    │           │ 136 (13.6)   │
╘═══════════════════╧══════╧═══════════╧══════════════╛

@tompollard
Copy link
Owner Author

It would be good to left align the index columns in the dataframe. See discussion on how to achieve this with styler at: pandas-dev/pandas#39602

Currently the columns are centered when rendered in a notebook, which looks awkward:

Screenshot 2023-05-02 at 3 52 34 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant