tompollard / tableone

Create "Table 1" for research papers in Python
https://pypi.python.org/pypi/tableone/
MIT License
161 stars 38 forks source link

add option to calculate "n(%)" percentages over a row. Ref #108 #110

Closed tompollard closed 3 years ago

tompollard commented 3 years ago

Addresses #108 by adding a row_percent argument. If row_percent = True then the percentage for categorical variables will be calculated over the row (rather than column).

from tableone import TableOne, load_dataset
data = load_dataset('pn2012')

columns = ['Age', 'SysABP', 'Height', 'MechVent', 'ICU', 'death']

# columns containing categorical variables
categorical = ['ICU', 'MechVent']

# set decimal places for age to 0
decimals = {"Age": 0}

# non-normal variables
nonnormal = ['Age']

# optionally, a categorical variable for stratification
groupby = ['death']

t1 = TableOne(data, columns=columns,  categorical=categorical, groupby=groupby,
                        nonnormal=nonnormal, decimals=decimals, row_percent=True)

print(t1.tabulate(tablefmt = "github"))
Missing Overall 0 1
n 1000 864 136
Age, median [Q1,Q3] 0 68 [53,79] 66 [53,78] 75 [62,83]
SysABP, mean (SD) 291 114.3 (40.2) 115.4 (38.3) 107.6 (49.4)
Height, mean (SD) 475 170.1 (22.1) 170.3 (23.2) 168.5 (11.3)
MechVent, n (%) 0 0 540 (100.0) 468 (86.7) 72 (13.3)
1 460 (100.0) 396 (86.1) 64 (13.9)
ICU, n (%) CCU 0 162 (100.0) 137 (84.6) 25 (15.4)
CSRU 202 (100.0) 194 (96.0) 8 (4.0)
MICU 380 (100.0) 318 (83.7) 62 (16.3)
SICU 256 (100.0) 215 (84.0) 41 (16.0)