tompollard / tableone

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

can't include groupby variable #112

Open jakob1379 opened 3 years ago

jakob1379 commented 3 years ago

Hello there. Your tool is near perfect for my investigations but I have one problem which is that I can't include the group by column in the table as well.

params = pd.read_spss("params.sav")
columns = ["Gender",'Adeno_vs_sq_vs_other']
categorical = ['Gender','Adeno_vs_sq_vs_other']
pval = True
groupby = 'Adeno_vs_sq_vs_other'
label_suffix = True

table = TableOne(data=params,
                     columns=columns,
                     groupby=groupby,
                     label_suffix=label_suffix,
                     pval=pval,
                     display_all=True,
                    rename=rename)
===>
                     Grouped by Histopathelogic type                                           
                                             Missing     Overall       adeno         sq P-Value
n                                                            342         205        137        
Gender, n (%) Female                               0  144 (42.1)   92 (44.9)  52 (38.0)   0.247
              Male                                    198 (57.9)  113 (55.1)  85 (62.0)        

Is there a flag I forget to set or is this not yet implemented?

jakob1379 commented 3 years ago

I know I'ts possible to just duplicate the column and include that, but it is not as elegant.

tompollard commented 1 year ago

@jakob1379 I'm sorry for the loooong wait for a reply, and I'm guessing you've long passed caring about the answer!

If you would like to include a row for Adeno_vs_sq_vs_other, you should be able to just add it to your list of columns. I'm unclear why it isn't being displayed in your example.

e.g.:

from tableone import TableOne, load_dataset

data = load_dataset('pn2012')

columns = ['ICU', 'Age']
categorical=['ICU']
table = TableOne(data, columns=columns, categorical=categorical, groupby="ICU")

print(table.tabulate(tablefmt="github"))

...outputs a table with "ICU" as the groupby variable and as a row:

Missing Overall CCU CSRU MICU SICU
n 1000 162 202 380 256
ICU, n (%) CCU 0 162 (16.2) 162 (100.0)
CSRU 202 (20.2) 202 (100.0)
MICU 380 (38.0) 380 (100.0)
SICU 256 (25.6) 256 (100.0)
Age, mean (SD) 0 65.0 (17.2) 71.6 (13.7) 67.1 (13.2) 63.7 (17.8) 61.2 (19.6)

I'll keep this issue open so we can investigate why it didn't work for you. I'll begin by finding an example where the row isn't rendered, and will then try to work out a fix.

jakob1379 commented 1 year ago

I no longer possess the data as it was for a research project. But thanks for keeping up the food work. If I can find some time I'll do some digging and see if I can find a similar working example.