Skip to content

Commit

Permalink
add tabulate output. ref #75
Browse files Browse the repository at this point in the history
  • Loading branch information
tompollard committed Nov 18, 2019
1 parent 900bbfb commit 061b9a5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 26 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ labels={'death': 'mortality'}
mytable = TableOne(data, columns, categorical, groupby, nonnormal, rename=labels, pval=False)
```

7. Type the name of the instance in an interpreter:
7. Display the table using the `tabulate` method. The `tablefmt` argument allows the table to be displayed in multiple formats, including "github", "grid", "fancy_grid", "rst", "html", and "latex".

```python
mytable
print(overall_table.tabulate(tablefmt = "fancy_grid"))
```

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

Grouped by mortality:

| variable | level | missing | 0 | 1 |
| | | Missing | 0 | 1 |
| --------- | ------ | -------- | -------------- | -------------- |
| n | | | 864 | 136 |
| Age | | 0 | 66 [52,78] | 75 [62,83] |
Expand All @@ -105,5 +105,3 @@ Grouped by mortality:
```python
mytable.to_excel('mytable.xlsx')
```


4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ Example

mytable = TableOne(data, columns, categorical, groupby, nonnormal)

#. Type the name of the instance in an interpreter::
#. Display the table using the ``tabulate`` method. The ``tablefmt`` argument allows the table to be displayed in multiple formats, including "github", "grid", "fancy_grid", "rst", "html", and "latex".::

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

#. ...which prints the following table to screen::

Expand Down
77 changes: 59 additions & 18 deletions tableone.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,47 @@
"- if `label_suffix=True`, \"mean (SD); n (%);\" etc are appended to the row label."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Displaying the table in custom formats\n",
"\n",
"The tableone object includes a `tabulate` method, that makes use of the [tabulate](https://pypi.org/project/tabulate/) package to display the table in custom output formats. Supported table formats include: \"github\", \"grid\", \"fancy_grid\", \"rst\", \"html\", \"latex\", and \"latex_raw\". See the tabulate package for more formats."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| | | Missing | Overall |\n",
"|-------------------|------|-----------|--------------|\n",
"| n | | | 1000 |\n",
"| Age, mean (SD) | | 0 | 65.0 (17.2) |\n",
"| SysABP, mean (SD) | | 291 | 114.3 (40.2) |\n",
"| Height, mean (SD) | | 475 | 170.1 (22.1) |\n",
"| Weight, mean (SD) | | 302 | 82.9 (23.8) |\n",
"| ICU, n (%) | CCU | 0 | 162 (16.2) |\n",
"| | CSRU | | 202 (20.2) |\n",
"| | MICU | | 380 (38.0) |\n",
"| | SICU | | 256 (25.6) |\n",
"| MechVent, n (%) | 0 | 0 | 540 (54.0) |\n",
"| | 1 | | 460 (46.0) |\n",
"| LOS, mean (SD) | | 0 | 14.2 (14.2) |\n",
"| death, n (%) | 0 | 0 | 864 (86.4) |\n",
"| | 1 | | 136 (13.6) |\n"
]
}
],
"source": [
"print(overall_table.tabulate(tablefmt = \"github\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -411,7 +452,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -420,7 +461,7 @@
"(-30, 250)"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
Expand Down Expand Up @@ -454,7 +495,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -493,7 +534,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -607,7 +648,7 @@
"[3] Warning, test for normality reports non-normal distributions for: Age, SysABP, Height, Weight."
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -656,7 +697,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -669,7 +710,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -679,7 +720,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -811,7 +852,7 @@
"[3] Warning, test for normality reports non-normal distributions for: Age, Height, SysABP, Weight."
]
},
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -841,7 +882,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -851,7 +892,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -884,8 +925,8 @@
" <th>Missing</th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>pval</th>\n",
" <th>ptest</th>\n",
" <th>p-value</th>\n",
" <th>Test</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
Expand Down Expand Up @@ -990,7 +1031,7 @@
],
"text/plain": [
" Grouped by death \n",
" Missing 0 1 pval ptest\n",
" Missing 0 1 p-value Test\n",
"n 864 136 \n",
"Age, median [Q1,Q3] 0 66.0 [52.8,78.0] 75.0 [62.0,83.0] <0.001 Kruskal-Wallis\n",
"SysABP, mean (SD) 291 115.4 (38.3) 107.6 (49.4) 0.134 Two Sample T-test\n",
Expand All @@ -1007,7 +1048,7 @@
"[3] Warning, test for normality reports non-normal distributions for: Age, Height, SysABP, Weight."
]
},
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1042,7 +1083,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1053,7 +1094,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1064,7 +1105,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand Down
38 changes: 37 additions & 1 deletion tableone.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pandas as pd
from scipy import stats
from statsmodels.stats import multitest
from tabulate import tabulate

import modality

Expand Down Expand Up @@ -226,6 +227,41 @@ def __repr__(self):
def _repr_html_(self):
return self.tableone._repr_html_() + self._generate_remark_str('<br />')

def tabulate(self, headers=None, tablefmt='grid', **kwargs):
"""
Pretty-print tableone data. Wrapper for the Python 'tabulate' library.
Args:
headers (list): Defines a list of column headers to be used.
tablefmt (str): Defines how the table is formatted. Table formats
include: 'plain','simple','github','grid','fancy_grid','pipe',
'orgtbl','jira','presto','psql','rst','mediawiki','moinmoin',
'youtrack','html','latex','latex_raw','latex_booktabs',
and 'textile'.
Examples:
To output tableone in github syntax, call tabulate with the
'tablefmt=github' argument.
>>> print(tableone.tabulate(tablefmt='fancy_grid'))
"""
# reformat table for tabulate
df = self.tableone

if not headers:
try:
headers = df.columns.levels[1]
except AttributeError:
headers = df.columns

df = df.reset_index()
df = df.set_index('level_0')
isdupe = df.index.duplicated()
df.index = df.index.where(~isdupe, '')
df = df.rename_axis(None).rename(columns={'level_1': ''})

return tabulate(df, headers=headers, tablefmt=tablefmt, **kwargs)

def _generate_remark_str(self, end_of_line='\n'):
"""
Generate a series of remarks that the user should consider
Expand Down Expand Up @@ -886,7 +922,7 @@ def _create_tableone(self, data):
pass

# remove the 'variable, value' column names in the index
table = table.rename_axis([None,None])
table = table.rename_axis([None, None])

return table

Expand Down

0 comments on commit 061b9a5

Please sign in to comment.