Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Pretty csv output. #121

Open
toshiakiasakura opened this issue May 9, 2022 · 2 comments
Open

Feature request: Pretty csv output. #121

toshiakiasakura opened this issue May 9, 2022 · 2 comments

Comments

@toshiakiasakura
Copy link

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
Copy link
Author

toshiakiasakura commented May 9, 2022

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
Copy link
Author

More elegant with this
Pandas multi-index to csv file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant