tableone icon indicating copy to clipboard operation
tableone copied to clipboard

Feature request: Pretty csv output.

Open toshiakiasakura opened this issue 2 years ago • 2 comments

Currently, I use pandoc to embed results of tableone to markdown file.

But if we export csv, there are duplicated names for index and column part, so I want tabulate method to include pretty style of csv export.

mean currently csv export becomes like

                         Missing    Overall
-----------------  ----  ---------  ------------
n                                   1000
Age, mean (SD)           0          65.0 (17.2)
SysABP, mean (SD)        291        114.3 (40.2)
Height, mean (SD)        475        170.1 (22.1)
Weight, mean (SD)        302        82.9 (23.8)
ICU, n (%)         CCU   0          162 (16.2)
ICU, n (%)                    CSRU             202 (20.2)
ICU, n (%)                    MICU             380 (38.0)
ICU, n (%)                    SICU             256 (25.6)
MechVent, n (%)    0     0          540 (54.0)
MechVent, n (%)                  1                460 (46.0)
LOS, mean (SD)           0          14.2 (14.2)
death, n (%)       0     0          864 (86.4)
death, n (%)                    1                136 (13.6)

but want to delete duplicated ICU, MechVent, and death

toshiakiasakura avatar May 09 '22 12:05 toshiakiasakura

I suggest like this way.

data preparation and defin MyTableOne.

# import libraries
from tableone import TableOne
import pandas as pd

from io import StringIO
import csv
import copy

class MyTableOne(TableOne):
    def to_pretty_csv(self, path : str = None):
        tableone = copy.deepcopy(self.tableone)
        tableone.columns = tableone.columns.droplevel(0)
        s = tableone.to_csv()
        f = StringIO(s)
        reader = csv.reader(f, delimiter=',')
        dup = []
        new_rows = ""
        for row in reader:
            r0 = row[0]
            if r0 == "":
                pass
            elif r0 not in dup:
                dup.append(r0)
            else:
                row[0] = ""
            new_rows += ",".join(row) + "\n"
        if path is None:
            return new_rows
        else:
            with open(path, "w") as f:
                f.write(new_rows)

# 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)
table = MyTableOne(data, groupby=["status"],label_suffix=True)

and processing part. Look for "sex" index, this code can delete there.

print(table.to_pretty_csv())

toshiakiasakura avatar May 09 '22 12:05 toshiakiasakura

More elegant with this Pandas multi-index to csv file

toshiakiasakura avatar Jun 02 '22 00:06 toshiakiasakura