Skip to main content

Table One

Project description

https://travis-ci.org/tompollard/tableone.svg?branch=master

tableone is a package for researchers who need to create Table 1, summary statistics for a patient population. It was inspired by the R package of the same name by Yoshida and Bohn. A demo Jupyter Notebook is available at: https://github.com/tompollard/tableone/blob/master/tableone.ipynb

Installation

The distribution is hosted on PyPI and directly installable via pip without needing to clone or download this repository. To install the package from PyPI, run the following command in your terminal:

pip install tableone

Example

  1. Import libraries:

    from tableone import TableOne
    import pandas as pd
  2. Load sample data into a pandas dataframe:

    url="https://raw.githubusercontent.com/tompollard/data/master/primary-biliary-cirrhosis/pbc.csv"
    data=pd.read_csv(url)
  3. List of columns containing continuous variables:

    convars = ['time','age','bili','chol','albumin','copper',
           'alk.phos','ast','trig','platelet','protime']
  4. List of columns containing categorical variables:

    catvars = ['status', 'ascites', 'hepato', 'spiders', 'edema',
           'stage', 'sex']
  5. Optionally, a categorical variable for stratification and a list of non-normal variables:

    strat = 'trt'
    nonnormal = ['bili']
  6. Create an instance of TableOne with the input arguments:

    mytable = TableOne(data, convars, catvars, strat, nonnormal)
  7. Type the name of the instance in an interpreter:

    mytable
  8. …which prints the following table to screen:

    Stratified by trt
                           1.0                2.0
    ---------------------  -----------------  -----------------
    n                      158                154
    time (mean (std))      2015.62 (1094.12)  1996.86 (1155.93)
    age (mean (std))       51.42 (11.01)      48.58 (9.96)
    bili (median [IQR])    1.40 [0.80,3.20]   1.30 [0.72,3.60]
    chol (mean (std))      365.01 (209.54)    373.88 (252.48)
    albumin (mean (std))   3.52 (0.44)        3.52 (0.40)
    copper (mean (std))    97.64 (90.59)      97.65 (80.49)
    alk.phos (mean (std))  2021.30 (2183.44)  1943.01 (2101.69)
    ast (mean (std))       120.21 (54.52)     124.97 (58.93)
    trig (mean (std))      124.14 (71.54)     125.25 (58.52)
    platelet (mean (std))  258.75 (100.32)    265.20 (90.73)
    protime (mean (std))   10.65 (0.85)       10.80 (1.14)
    status (n (%))
    0                      83 (52.53)         85 (55.19)
    1                      10 (6.33)          9 (5.84)
    2                      65 (41.14)         60 (38.96)
    ascites (n (%))
    0.0                    144 (91.14)        144 (93.51)
    1.0                    14 (8.86)          10 (6.49)
    hepato (n (%))
    0.0                    85 (53.80)         67 (43.51)
    1.0                    73 (46.20)         87 (56.49)
    spiders (n (%))
    0.0                    113 (71.52)        109 (70.78)
    1.0                    45 (28.48)         45 (29.22)
    edema (n (%))
    0.0                    132 (83.54)        131 (85.06)
    0.5                    16 (10.13)         13 (8.44)
    1.0                    10 (6.33)          10 (6.49)
    stage (n (%))
    1.0                    12 (7.59)          4 (2.60)
    2.0                    35 (22.15)         32 (20.78)
    3.0                    56 (35.44)         64 (41.56)
    4.0                    55 (34.81)         54 (35.06)
    sex (n (%))
    f                      137 (86.71)        139 (90.26)
    m                      21 (13.29)         15 (9.74)
  9. Compute p values by setting the pval argument to True. The name of the test that was used is also displayed:

    mytable = TableOne(data, convars, catvars, strat, nonnormal, pval=True)
  10. …which prints:

    Stratified by trt
                           1.0                2.0                pval    testname
    ---------------------  -----------------  -----------------  ------  --------------
    n                      158                154
    time (mean (std))      2015.62 (1094.12)  1996.86 (1155.93)  0.883   One_way_ANOVA
    age (mean (std))       51.42 (11.01)      48.58 (9.96)       0.018   One_way_ANOVA
    bili (median [IQR])    1.40 [0.80,3.20]   1.30 [0.72,3.60]   0.842   Kruskal-Wallis
    chol (mean (std))      365.01 (209.54)    373.88 (252.48)    0.748   One_way_ANOVA
    albumin (mean (std))   3.52 (0.44)        3.52 (0.40)        0.874   One_way_ANOVA
    copper (mean (std))    97.64 (90.59)      97.65 (80.49)      0.999   One_way_ANOVA
    alk.phos (mean (std))  2021.30 (2183.44)  1943.01 (2101.69)  0.747   One_way_ANOVA
    ast (mean (std))       120.21 (54.52)     124.97 (58.93)     0.460   One_way_ANOVA
    trig (mean (std))      124.14 (71.54)     125.25 (58.52)     0.886   One_way_ANOVA
    platelet (mean (std))  258.75 (100.32)    265.20 (90.73)     0.555   One_way_ANOVA
    protime (mean (std))   10.65 (0.85)       10.80 (1.14)       0.197   One_way_ANOVA
    status (n (%))                                               0.894   Chi-squared
    0                      83 (52.53)         85 (55.19)
    1                      10 (6.33)          9 (5.84)
    2                      65 (41.14)         60 (38.96)
    ascites (n (%))                                              0.567   Chi-squared
    0.0                    144 (91.14)        144 (93.51)
    1.0                    14 (8.86)          10 (6.49)
    hepato (n (%))                                               0.088   Chi-squared
    0.0                    85 (53.80)         67 (43.51)
    1.0                    73 (46.20)         87 (56.49)
    spiders (n (%))                                              0.985   Chi-squared
    0.0                    113 (71.52)        109 (70.78)
    1.0                    45 (28.48)         45 (29.22)
    edema (n (%))                                                0.877   Chi-squared
    0.0                    132 (83.54)        131 (85.06)
    0.5                    16 (10.13)         13 (8.44)
    1.0                    10 (6.33)          10 (6.49)
    stage (n (%))                                                0.201   Chi-squared
    1.0                    12 (7.59)          4 (2.60)
    2.0                    35 (22.15)         32 (20.78)
    3.0                    56 (35.44)         64 (41.56)
    4.0                    55 (34.81)         54 (35.06)
    sex (n (%))                                                  0.421   Chi-squared
    f                      137 (86.71)        139 (90.26)
    m                      21 (13.29)         15 (9.74)
  11. Tables can be exported to file in various formats, including LaTeX, Markdown, CSV, and HTML. Files are exported by calling the to_format methods. For example, mytable can be exported to a CSV named ‘mytable.csv’ with the following command:

    mytable.to_csv('mytable.csv')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tableone-0.2.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

tableone-0.2.0-py2.py3-none-any.whl (9.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tableone-0.2.0.tar.gz.

File metadata

  • Download URL: tableone-0.2.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tableone-0.2.0.tar.gz
Algorithm Hash digest
SHA256 896e9b928a0d61c797944b9b7aafcb14d9ad7a0aea6f35173e2258fc2f919877
MD5 ab3733f06c4dd8a8fdf3ce56d48abef3
BLAKE2b-256 16e741eba829ccb50c1cd21fcfb95bedda3f1888f1632702bd1863278d88fa79

See more details on using hashes here.

File details

Details for the file tableone-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for tableone-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f0f0889877186c60b4d819ad016dbdd9443e3e9b1ab4a4f12d2a1e650d33b55b
MD5 57ef6e43e72c5b6502631f53f85a2bba
BLAKE2b-256 b776f7c6d2165a15cff4203d274dcc980c7a72de88eb9a7b7d4645cfc4cb5f0a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page