tompollard / tableone

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

Sort by p-value #90

Closed plumdeq closed 4 years ago

plumdeq commented 4 years ago

Thanks for a fantastic library. Do you know if it is possible to sort by the p-value? Currently I have to write the whole table to csv, then load it as a Pandas DataFrame, and then sort it. Sorting the rows with categorial variables is, however, not possible in this case...

tompollard commented 4 years ago

Thanks @plumdeq :) We'll try to look at p-value sorting and #83 in the next few days.

tompollard commented 4 years ago

Not got around to this yet (sorry), but one thing to note is that the pd.DataFrame object can be accessed with the tableone attribute (e.g. mytable.tableone), so there shouldn't be a need to do your custom sort by writing to CSV first.

plumdeq commented 4 years ago

(Double) Thank you, Tom!

tompollard commented 4 years ago

Version 0.6.6 allows you to sort by the following columns:

Hope this helps, and give me a shout if you hit any issues! Example below:

# import libraries
from tableone import TableOne
import pandas as pd

# load sample data into a pandas dataframe
url="https://raw.githubusercontent.com/tompollard/tableone/master/data/pn2012_demo.csv"
data=pd.read_csv(url)

# columns to summarize
columns = ['SysABP', 'Height', 'Weight', 'ICU']

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

# create tableone with the input arguments
mytable = TableOne(data, columns=columns, categorical=categorical, 
                   label_suffix=True, groupby = "death", pval=True,
                   sort="P-Value")

print(mytable.tabulate(tablefmt = "github"))
Missing 0 1 P-Value Test
n 864 136
ICU, n (%) CCU 0 137 (15.9) 25 (18.4) <0.001 Chi-squared
CSRU 194 (22.5) 8 (5.9)
MICU 318 (36.8) 62 (45.6)
SICU 215 (24.9) 41 (30.1)
SysABP, mean (SD) 291 115.4 (38.3) 107.6 (49.4) 0.134 Two Sample T-test
Height, mean (SD) 475 170.3 (23.2) 168.5 (11.3) 0.304 Two Sample T-test
Weight, mean (SD) 302 83.0 (23.6) 82.3 (25.4) 0.782 Two Sample T-test