Open toshiakiasakura opened 2 years ago
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())
More elegant with this Pandas multi-index to csv file
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
but want to delete duplicated ICU, MechVent, and death