tompollard / tableone

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

Binary Variables #111

Open jpoles1 opened 3 years ago

jpoles1 commented 3 years ago

It would be nice if there was a way to display binary variables, other than making them categorical, in which case both True and False are shown. The ideal would be to have just the values for the "True" category displayed, I think it would look far more professional!

image

jpoles1 commented 3 years ago

I tried looking into the TableOne code a bit, and it's looking like a PR is something I don't have quite enough time for right now, unfortunately. For anyone looking for a quick and dirty solution, you can do the following:

table1 = TableOne()
table1 = table1.tableone #access underlying pandas df
table1 = table1[table1.index.get_level_values(1) != "False"] #Remove rows with "False"
table1.rename(index={"True": ""}, inplace=True) #Rename "True" to blank

and now it works perfectly!

image

PS: If you want to make your reformatted tables look pretty, you can use some css styles as below:

table1 = table1.style.set_table_styles(table_styles)
display(HTML(table1.render()))
tompollard commented 3 years ago

Thanks @jpoles1, we'll take a look at implementing this!

raheems commented 3 years ago

You wouldn't believe that I started to post this exact feature request a few days ago and then decided not to post it :)

I did solve it using pandas as suggested. But I would appreciate if this feature is added :)

jpoles1 commented 3 years ago

Thanks @jpoles1, we'll take a look at implementing this!

Awesome, thanks such much for the great work on this lib!

dnatanov commented 3 years ago

Is there a way to include p values when only displaying the True binary variables? When I used the "quick and dirty" method to only include the binary variables listed as True, the calculated p values were removed from the table.