tompollard / tableone

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

Support multilevel groupby #18

Open tompollard opened 6 years ago

tompollard commented 6 years ago

It would be good to support multilevel groupby arguments. e.g. groupby = ['icu_type','gender'].

e.g.: achieve something similar to:

from tableone import TableOne
import pandas as pd

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

df = TableOne(data)

pd.concat([df.tableone,df.tableone], axis=1)

Notes:

# columns to be summarized
columns = ['Age', 'SysABP', 'Height', 'Weight', 'ICU', 'death']

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

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

# alternative labels
labels={'LOS': 'Length of stay', 'death': 'mortality'}

groupby = ['death']
second = 'ICU'
tdict = {}

for d in data[second].unique():
    print(d)
    tdict[d] =  TableOne(data[data[second] == d], columns, categorical, groupby, nonnormal)

pd.concat([tdict['SICU'].tableone,tdict['CSRU'].tableone,tdict['MICU'].tableone],axis=1)

pd.concat([tdict[x].tableone for x in data[second].unique()],axis=1)