02. Basic example

Basic usage of the tableone library.

 7 # Libraries
 8 import pandas as pd
 9
10 # Specific
11 from pathlib import Path
12 from tableone import TableOne
13
14
15 # ------------------------
16 # Load data
17 # ------------------------
18 # Load data
19 path = Path('../../datasets/dengue-htd-dataset')
20 data = pd.read_csv(path / 'dengue.csv')
21
22 # ------------------------
23 # Create tableone
24 # ------------------------
25 # Columns
26 columns = ['age', 'gender', 'haematocrit_percent', 'plt']
27
28 # Categorical
29 categorical = ['gender']
30
31 # Groupby
32 groupby = ['cvs_hos_split']
33
34 #
35 mytable = TableOne(data, columns, categorical, groupby)

Show

41 mytable.tableone
Grouped by cvs_hos_split
Missing Overall cvs hos
n 525 393 132
age, mean (SD) 0 37.3 (16.3) 37.4 (16.3) 36.9 (16.5)
gender, n (%) 0 0 249 (47.4) 190 (48.3) 59 (44.7)
1 276 (52.6) 203 (51.7) 73 (55.3)
haematocrit_percent, mean (SD) 0 38.5 (8.6) 38.2 (8.6) 39.3 (8.7)
plt, mean (SD) 0 106.5 (132.5) 110.8 (146.6) 93.8 (75.9)


Show (HTML)

Html

47 html = mytable.to_html()
48
49 # show
50 print(html)

Out:

<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th></th>
      <th colspan="4" halign="left">Grouped by cvs_hos_split</th>
    </tr>
    <tr>
      <th></th>
      <th></th>
      <th>Missing</th>
      <th>Overall</th>
      <th>cvs</th>
      <th>hos</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>n</th>
      <th></th>
      <td></td>
      <td>525</td>
      <td>393</td>
      <td>132</td>
    </tr>
    <tr>
      <th>age, mean (SD)</th>
      <th></th>
      <td>0</td>
      <td>37.3 (16.3)</td>
      <td>37.4 (16.3)</td>
      <td>36.9 (16.5)</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">gender, n (%)</th>
      <th>0</th>
      <td>0</td>
      <td>249 (47.4)</td>
      <td>190 (48.3)</td>
      <td>59 (44.7)</td>
    </tr>
    <tr>
      <th>1</th>
      <td></td>
      <td>276 (52.6)</td>
      <td>203 (51.7)</td>
      <td>73 (55.3)</td>
    </tr>
    <tr>
      <th>haematocrit_percent, mean (SD)</th>
      <th></th>
      <td>0</td>
      <td>38.5 (8.6)</td>
      <td>38.2 (8.6)</td>
      <td>39.3 (8.7)</td>
    </tr>
    <tr>
      <th>plt, mean (SD)</th>
      <th></th>
      <td>0</td>
      <td>106.5 (132.5)</td>
      <td>110.8 (146.6)</td>
      <td>93.8 (75.9)</td>
    </tr>
  </tbody>
</table>

Total running time of the script: ( 0 minutes 0.133 seconds)

Gallery generated by Sphinx-Gallery