tompollard / tableone

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

tableone columns should be output in a consistent order #37

Closed alistairewj closed 6 years ago

alistairewj commented 6 years ago

Column order at the moment depends on the group names when using groupby.

url="https://raw.githubusercontent.com/tompollard/data/master/primary-biliary-cirrhosis/pbc.csv"
data_pbc=pd.read_csv(url)
table = TableOne(data_pbc, columns=columns, groupby=groupby, pval=True, pval_adjust='bonferroni')
table

gives:

variable level f isnull m ptest pval (adjusted)
n 374   44    
age 50.16 (10.24) 0 55.71 (10.98) One-way ANOVA 0.002
albumin 3.49 (0.42) 0 3.53 (0.46) One-way ANOVA 0.785
ast 122.63 (57.92) 106 121.99 (47.01) One-way ANOVA 0.949

... while

url="https://raw.githubusercontent.com/tompollard/data/master/primary-biliary-cirrhosis/pbc.csv"
data_pbc=pd.read_csv(url)
df = data_pbc.copy()
df.loc[df['sex']=='f', 'sex'] = 'q'
table = TableOne(df, columns=columns, groupby=groupby, pval=True, pval_adjust='bonferroni')
table

gives:

variable level isnull m ptest pval (adjusted) q
n   44     374
age 0 55.71 (10.98) One-way ANOVA 0.002 50.16 (10.24)
albumin 0 3.53 (0.46) One-way ANOVA 0.785 3.49 (0.42)
ast 106 121.99 (47.01) One-way ANOVA 0.949 122.63 (57.92)