Note
Click here to download the full example code
01. Basic example
Basic usage of the tableone library.
8 # Libraries
9 import pandas as pd
10
11 from tableone import TableOne
12
13 # Load data
14 url="https://raw.githubusercontent.com/tompollard/data/master/primary-biliary-cirrhosis/pbc.csv"
15 data=pd.read_csv(url)
16
17 # List of columns
18 columns = [
19 'age', 'bili', 'albumin', 'ast',
20 'platelet', 'protime', 'ascites',
21 'hepato', 'spiders', 'edema', 'sex',
22 'trt'
23 ]
24
25 # Specify categorical columns
26 categorical = ['ascites','hepato','edema','sex','spiders','trt']
27
28 # Define groupby and not normal
29 groupby = 'trt'
30 nonnormal = ['bili']
31
32 # Create descriptive table
33 mytable = TableOne(data, columns, categorical,
34 groupby, nonnormal, pval=True)
38 mytable.tableone
Convert to html
42 html = mytable.to_html()
43 html
Out:
'<table border="1" class="dataframe">\n <thead>\n <tr>\n <th></th>\n <th></th>\n <th colspan="5" halign="left">Grouped by trt</th>\n </tr>\n <tr>\n <th></th>\n <th></th>\n <th>Missing</th>\n <th>Overall</th>\n <th>1.0</th>\n <th>2.0</th>\n <th>P-Value</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>n</th>\n <th></th>\n <td></td>\n <td>418</td>\n <td>158</td>\n <td>154</td>\n <td></td>\n </tr>\n <tr>\n <th>age, mean (SD)</th>\n <th></th>\n <td>0</td>\n <td>50.7 (10.4)</td>\n <td>51.4 (11.0)</td>\n <td>48.6 (10.0)</td>\n <td>0.018</td>\n </tr>\n <tr>\n <th>bili, median [Q1,Q3]</th>\n <th></th>\n <td>0</td>\n <td>1.4 [0.8,3.4]</td>\n <td>1.4 [0.8,3.2]</td>\n <td>1.3 [0.7,3.6]</td>\n <td>0.842</td>\n </tr>\n <tr>\n <th>albumin, mean (SD)</th>\n <th></th>\n <td>0</td>\n <td>3.5 (0.4)</td>\n <td>3.5 (0.4)</td>\n <td>3.5 (0.4)</td>\n <td>0.874</td>\n </tr>\n <tr>\n <th>ast, mean (SD)</th>\n <th></th>\n <td>106</td>\n <td>122.6 (56.7)</td>\n <td>120.2 (54.5)</td>\n <td>125.0 (58.9)</td>\n <td>0.460</td>\n </tr>\n <tr>\n <th>platelet, mean (SD)</th>\n <th></th>\n <td>11</td>\n <td>257.0 (98.3)</td>\n <td>258.8 (100.3)</td>\n <td>265.2 (90.7)</td>\n <td>0.554</td>\n </tr>\n <tr>\n <th>protime, mean (SD)</th>\n <th></th>\n <td>2</td>\n <td>10.7 (1.0)</td>\n <td>10.7 (0.9)</td>\n <td>10.8 (1.1)</td>\n <td>0.199</td>\n </tr>\n <tr>\n <th rowspan="2" valign="top">ascites, n (%)</th>\n <th>0.0</th>\n <td>106</td>\n <td>288 (92.3)</td>\n <td>144 (91.1)</td>\n <td>144 (93.5)</td>\n <td>0.567</td>\n </tr>\n <tr>\n <th>1.0</th>\n <td></td>\n <td>24 (7.7)</td>\n <td>14 (8.9)</td>\n <td>10 (6.5)</td>\n <td></td>\n </tr>\n <tr>\n <th rowspan="2" valign="top">hepato, n (%)</th>\n <th>0.0</th>\n <td>106</td>\n <td>152 (48.7)</td>\n <td>85 (53.8)</td>\n <td>67 (43.5)</td>\n <td>0.088</td>\n </tr>\n <tr>\n <th>1.0</th>\n <td></td>\n <td>160 (51.3)</td>\n <td>73 (46.2)</td>\n <td>87 (56.5)</td>\n <td></td>\n </tr>\n <tr>\n <th rowspan="2" valign="top">spiders, n (%)</th>\n <th>0.0</th>\n <td>106</td>\n <td>222 (71.2)</td>\n <td>113 (71.5)</td>\n <td>109 (70.8)</td>\n <td>0.985</td>\n </tr>\n <tr>\n <th>1.0</th>\n <td></td>\n <td>90 (28.8)</td>\n <td>45 (28.5)</td>\n <td>45 (29.2)</td>\n <td></td>\n </tr>\n <tr>\n <th rowspan="3" valign="top">edema, n (%)</th>\n <th>0.0</th>\n <td>0</td>\n <td>354 (84.7)</td>\n <td>132 (83.5)</td>\n <td>131 (85.1)</td>\n <td>0.877</td>\n </tr>\n <tr>\n <th>0.5</th>\n <td></td>\n <td>44 (10.5)</td>\n <td>16 (10.1)</td>\n <td>13 (8.4)</td>\n <td></td>\n </tr>\n <tr>\n <th>1.0</th>\n <td></td>\n <td>20 (4.8)</td>\n <td>10 (6.3)</td>\n <td>10 (6.5)</td>\n <td></td>\n </tr>\n <tr>\n <th rowspan="2" valign="top">sex, n (%)</th>\n <th>f</th>\n <td>0</td>\n <td>374 (89.5)</td>\n <td>137 (86.7)</td>\n <td>139 (90.3)</td>\n <td>0.421</td>\n </tr>\n <tr>\n <th>m</th>\n <td></td>\n <td>44 (10.5)</td>\n <td>21 (13.3)</td>\n <td>15 (9.7)</td>\n <td></td>\n </tr>\n <tr>\n <th rowspan="2" valign="top">trt, n (%)</th>\n <th>1.0</th>\n <td>106</td>\n <td>158 (50.6)</td>\n <td>158 (100.0)</td>\n <td></td>\n <td><0.001</td>\n </tr>\n <tr>\n <th>2.0</th>\n <td></td>\n <td>154 (49.4)</td>\n <td></td>\n <td>154 (100.0)</td>\n <td></td>\n </tr>\n </tbody>\n</table>'
Lets tabulate for github.
47 print(mytable.tabulate(tablefmt="github"))
Out:
| | | Missing | Overall | 1.0 | 2.0 | P-Value |
|----------------------|-----|-----------|---------------|---------------|---------------|-----------|
| n | | | 418 | 158 | 154 | |
| age, mean (SD) | | 0 | 50.7 (10.4) | 51.4 (11.0) | 48.6 (10.0) | 0.018 |
| bili, median [Q1,Q3] | | 0 | 1.4 [0.8,3.4] | 1.4 [0.8,3.2] | 1.3 [0.7,3.6] | 0.842 |
| albumin, mean (SD) | | 0 | 3.5 (0.4) | 3.5 (0.4) | 3.5 (0.4) | 0.874 |
| ast, mean (SD) | | 106 | 122.6 (56.7) | 120.2 (54.5) | 125.0 (58.9) | 0.460 |
| platelet, mean (SD) | | 11 | 257.0 (98.3) | 258.8 (100.3) | 265.2 (90.7) | 0.554 |
| protime, mean (SD) | | 2 | 10.7 (1.0) | 10.7 (0.9) | 10.8 (1.1) | 0.199 |
| ascites, n (%) | 0.0 | 106 | 288 (92.3) | 144 (91.1) | 144 (93.5) | 0.567 |
| | 1.0 | | 24 (7.7) | 14 (8.9) | 10 (6.5) | |
| hepato, n (%) | 0.0 | 106 | 152 (48.7) | 85 (53.8) | 67 (43.5) | 0.088 |
| | 1.0 | | 160 (51.3) | 73 (46.2) | 87 (56.5) | |
| spiders, n (%) | 0.0 | 106 | 222 (71.2) | 113 (71.5) | 109 (70.8) | 0.985 |
| | 1.0 | | 90 (28.8) | 45 (28.5) | 45 (29.2) | |
| edema, n (%) | 0.0 | 0 | 354 (84.7) | 132 (83.5) | 131 (85.1) | 0.877 |
| | 0.5 | | 44 (10.5) | 16 (10.1) | 13 (8.4) | |
| | 1.0 | | 20 (4.8) | 10 (6.3) | 10 (6.5) | |
| sex, n (%) | f | 0 | 374 (89.5) | 137 (86.7) | 139 (90.3) | 0.421 |
| | m | | 44 (10.5) | 21 (13.3) | 15 (9.7) | |
| trt, n (%) | 1.0 | 106 | 158 (50.6) | 158 (100.0) | | <0.001 |
| | 2.0 | | 154 (49.4) | | 154 (100.0) | |
Lets tabulate for latex.
51 print(mytable.tabulate(tablefmt="latex"))
Out:
\begin{tabular}{lllllll}
\hline
& & Missing & Overall & 1.0 & 2.0 & P-Value \\
\hline
n & & & 418 & 158 & 154 & \\
age, mean (SD) & & 0 & 50.7 (10.4) & 51.4 (11.0) & 48.6 (10.0) & 0.018 \\
bili, median [Q1,Q3] & & 0 & 1.4 [0.8,3.4] & 1.4 [0.8,3.2] & 1.3 [0.7,3.6] & 0.842 \\
albumin, mean (SD) & & 0 & 3.5 (0.4) & 3.5 (0.4) & 3.5 (0.4) & 0.874 \\
ast, mean (SD) & & 106 & 122.6 (56.7) & 120.2 (54.5) & 125.0 (58.9) & 0.460 \\
platelet, mean (SD) & & 11 & 257.0 (98.3) & 258.8 (100.3) & 265.2 (90.7) & 0.554 \\
protime, mean (SD) & & 2 & 10.7 (1.0) & 10.7 (0.9) & 10.8 (1.1) & 0.199 \\
ascites, n (\%) & 0.0 & 106 & 288 (92.3) & 144 (91.1) & 144 (93.5) & 0.567 \\
& 1.0 & & 24 (7.7) & 14 (8.9) & 10 (6.5) & \\
hepato, n (\%) & 0.0 & 106 & 152 (48.7) & 85 (53.8) & 67 (43.5) & 0.088 \\
& 1.0 & & 160 (51.3) & 73 (46.2) & 87 (56.5) & \\
spiders, n (\%) & 0.0 & 106 & 222 (71.2) & 113 (71.5) & 109 (70.8) & 0.985 \\
& 1.0 & & 90 (28.8) & 45 (28.5) & 45 (29.2) & \\
edema, n (\%) & 0.0 & 0 & 354 (84.7) & 132 (83.5) & 131 (85.1) & 0.877 \\
& 0.5 & & 44 (10.5) & 16 (10.1) & 13 (8.4) & \\
& 1.0 & & 20 (4.8) & 10 (6.3) & 10 (6.5) & \\
sex, n (\%) & f & 0 & 374 (89.5) & 137 (86.7) & 139 (90.3) & 0.421 \\
& m & & 44 (10.5) & 21 (13.3) & 15 (9.7) & \\
trt, n (\%) & 1.0 & 106 & 158 (50.6) & 158 (100.0) & & \ensuremath{<}0.001 \\
& 2.0 & & 154 (49.4) & & 154 (100.0) & \\
\hline
\end{tabular}
Lets tabulate for grid.
55 print(mytable.tabulate(tablefmt="grid"))
Out:
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | | Missing | Overall | 1.0 | 2.0 | P-Value |
+======================+=====+===========+===============+===============+===============+===========+
| n | | | 418 | 158 | 154 | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| age, mean (SD) | | 0 | 50.7 (10.4) | 51.4 (11.0) | 48.6 (10.0) | 0.018 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| bili, median [Q1,Q3] | | 0 | 1.4 [0.8,3.4] | 1.4 [0.8,3.2] | 1.3 [0.7,3.6] | 0.842 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| albumin, mean (SD) | | 0 | 3.5 (0.4) | 3.5 (0.4) | 3.5 (0.4) | 0.874 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| ast, mean (SD) | | 106 | 122.6 (56.7) | 120.2 (54.5) | 125.0 (58.9) | 0.460 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| platelet, mean (SD) | | 11 | 257.0 (98.3) | 258.8 (100.3) | 265.2 (90.7) | 0.554 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| protime, mean (SD) | | 2 | 10.7 (1.0) | 10.7 (0.9) | 10.8 (1.1) | 0.199 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| ascites, n (%) | 0.0 | 106 | 288 (92.3) | 144 (91.1) | 144 (93.5) | 0.567 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | 1.0 | | 24 (7.7) | 14 (8.9) | 10 (6.5) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| hepato, n (%) | 0.0 | 106 | 152 (48.7) | 85 (53.8) | 67 (43.5) | 0.088 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | 1.0 | | 160 (51.3) | 73 (46.2) | 87 (56.5) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| spiders, n (%) | 0.0 | 106 | 222 (71.2) | 113 (71.5) | 109 (70.8) | 0.985 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | 1.0 | | 90 (28.8) | 45 (28.5) | 45 (29.2) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| edema, n (%) | 0.0 | 0 | 354 (84.7) | 132 (83.5) | 131 (85.1) | 0.877 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | 0.5 | | 44 (10.5) | 16 (10.1) | 13 (8.4) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | 1.0 | | 20 (4.8) | 10 (6.3) | 10 (6.5) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| sex, n (%) | f | 0 | 374 (89.5) | 137 (86.7) | 139 (90.3) | 0.421 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | m | | 44 (10.5) | 21 (13.3) | 15 (9.7) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| trt, n (%) | 1.0 | 106 | 158 (50.6) | 158 (100.0) | | <0.001 |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
| | 2.0 | | 154 (49.4) | | 154 (100.0) | |
+----------------------+-----+-----------+---------------+---------------+---------------+-----------+
Lets tabulate for fancy grid.
59 print(mytable.tabulate(tablefmt="fancy_grid"))
Out:
╒══════════════════════╤═════╤═══════════╤═══════════════╤═══════════════╤═══════════════╤═══════════╕
│ │ │ Missing │ Overall │ 1.0 │ 2.0 │ P-Value │
╞══════════════════════╪═════╪═══════════╪═══════════════╪═══════════════╪═══════════════╪═══════════╡
│ n │ │ │ 418 │ 158 │ 154 │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ age, mean (SD) │ │ 0 │ 50.7 (10.4) │ 51.4 (11.0) │ 48.6 (10.0) │ 0.018 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ bili, median [Q1,Q3] │ │ 0 │ 1.4 [0.8,3.4] │ 1.4 [0.8,3.2] │ 1.3 [0.7,3.6] │ 0.842 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ albumin, mean (SD) │ │ 0 │ 3.5 (0.4) │ 3.5 (0.4) │ 3.5 (0.4) │ 0.874 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ ast, mean (SD) │ │ 106 │ 122.6 (56.7) │ 120.2 (54.5) │ 125.0 (58.9) │ 0.460 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ platelet, mean (SD) │ │ 11 │ 257.0 (98.3) │ 258.8 (100.3) │ 265.2 (90.7) │ 0.554 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ protime, mean (SD) │ │ 2 │ 10.7 (1.0) │ 10.7 (0.9) │ 10.8 (1.1) │ 0.199 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ ascites, n (%) │ 0.0 │ 106 │ 288 (92.3) │ 144 (91.1) │ 144 (93.5) │ 0.567 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ 1.0 │ │ 24 (7.7) │ 14 (8.9) │ 10 (6.5) │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ hepato, n (%) │ 0.0 │ 106 │ 152 (48.7) │ 85 (53.8) │ 67 (43.5) │ 0.088 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ 1.0 │ │ 160 (51.3) │ 73 (46.2) │ 87 (56.5) │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ spiders, n (%) │ 0.0 │ 106 │ 222 (71.2) │ 113 (71.5) │ 109 (70.8) │ 0.985 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ 1.0 │ │ 90 (28.8) │ 45 (28.5) │ 45 (29.2) │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ edema, n (%) │ 0.0 │ 0 │ 354 (84.7) │ 132 (83.5) │ 131 (85.1) │ 0.877 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ 0.5 │ │ 44 (10.5) │ 16 (10.1) │ 13 (8.4) │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ 1.0 │ │ 20 (4.8) │ 10 (6.3) │ 10 (6.5) │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ sex, n (%) │ f │ 0 │ 374 (89.5) │ 137 (86.7) │ 139 (90.3) │ 0.421 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ m │ │ 44 (10.5) │ 21 (13.3) │ 15 (9.7) │ │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ trt, n (%) │ 1.0 │ 106 │ 158 (50.6) │ 158 (100.0) │ │ <0.001 │
├──────────────────────┼─────┼───────────┼───────────────┼───────────────┼───────────────┼───────────┤
│ │ 2.0 │ │ 154 (49.4) │ │ 154 (100.0) │ │
╘══════════════════════╧═════╧═══════════╧═══════════════╧═══════════════╧═══════════════╧═══════════╛
Lets tabulate for markdown
63 print(mytable.tabulate(tablefmt="rst"))
Out:
==================== === ========= ============= ============= ============= =========
.. Missing Overall 1.0 2.0 P-Value
==================== === ========= ============= ============= ============= =========
n 418 158 154
age, mean (SD) 0 50.7 (10.4) 51.4 (11.0) 48.6 (10.0) 0.018
bili, median [Q1,Q3] 0 1.4 [0.8,3.4] 1.4 [0.8,3.2] 1.3 [0.7,3.6] 0.842
albumin, mean (SD) 0 3.5 (0.4) 3.5 (0.4) 3.5 (0.4) 0.874
ast, mean (SD) 106 122.6 (56.7) 120.2 (54.5) 125.0 (58.9) 0.460
platelet, mean (SD) 11 257.0 (98.3) 258.8 (100.3) 265.2 (90.7) 0.554
protime, mean (SD) 2 10.7 (1.0) 10.7 (0.9) 10.8 (1.1) 0.199
ascites, n (%) 0.0 106 288 (92.3) 144 (91.1) 144 (93.5) 0.567
.. 1.0 24 (7.7) 14 (8.9) 10 (6.5)
hepato, n (%) 0.0 106 152 (48.7) 85 (53.8) 67 (43.5) 0.088
.. 1.0 160 (51.3) 73 (46.2) 87 (56.5)
spiders, n (%) 0.0 106 222 (71.2) 113 (71.5) 109 (70.8) 0.985
.. 1.0 90 (28.8) 45 (28.5) 45 (29.2)
edema, n (%) 0.0 0 354 (84.7) 132 (83.5) 131 (85.1) 0.877
.. 0.5 44 (10.5) 16 (10.1) 13 (8.4)
.. 1.0 20 (4.8) 10 (6.3) 10 (6.5)
sex, n (%) f 0 374 (89.5) 137 (86.7) 139 (90.3) 0.421
.. m 44 (10.5) 21 (13.3) 15 (9.7)
trt, n (%) 1.0 106 158 (50.6) 158 (100.0) <0.001
.. 2.0 154 (49.4) 154 (100.0)
==================== === ========= ============= ============= ============= =========
Save as latex file
67 mytable.to_latex('./outputs/main01-mytable.tex')
Save as xls file
71 mytable.to_excel('./outputs/main01-mytable.xlsx')
Total running time of the script: ( 0 minutes 0.618 seconds)