groupby = 'Reoperation' categorical = [] for element in columns: if element not in remove_cols: categorical.append(element) mytable = TableOne(data=df, columns=list_columns, categorical=categorical, groupby=groupby, pval=True, smd=True, htest_name=True) #,min_max=remove_cols) #data, columns, categorical, groupby, nonnormal, pval = True, smd=True,htest_name=True mytable.to_csv("_table.csv") ############################################################################ ##merge df = pd.read_csv("op_first_table_table.csv") df1 = pd.read_csv("op_reop_table_table.csv") df = df.rename( columns={ 'Unnamed: 0': 'values', 'Unnamed: 1': 'type', 'Missing': 'Missing_first', 'Overall': 'first' }) df1 = df1.rename(
# In[276]: ## 非正态分布列名列表 print(mytable) """ 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”. """ # In[261]: mytable.to_csv('pythontable1.csv') # In[262]: help(TableOne) # In[ ]:
#Sort the data in the right samples data1.sort_values(by=['year'], inplace=True) df_1_t = data1.iloc[:94, :] #all the samples of 2019 df_2 = data1.iloc[95:, :] #all the samples of 2020 #drop_indices = np.random.choice(df_2_t.index, 3, replace=False) # make the sample sizes of 2020 and 2019 same length by deleting three random entries #df_2=df_2_t.drop(drop_indices) df_1 = df_1_t.drop(57) #drop the nan data sample df_1.sort_values(by=['living'], inplace=True) #sort by living situation df_2.sort_values(by=['living'], inplace=True) #sort by living situation #make table 1 columns = ['year', 'gender', 'bmi', 'living'] mytable = TableOne(data1, columns=columns, pval=False) print(mytable.tabulate(tablefmt="fancy_grid")) mytable.to_csv('mytable.csv') #split the data samples in living with their parents and moved out for 2020 and calculate means grouped1 = df_1.groupby(df_1.living) Moved_out_2019 = grouped1.get_group("Moved_out") mean1 = Moved_out_2019["attitu_2"].mean() Parents_2019 = grouped1.get_group("Living_with_parents") mean2 = Parents_2019["attitu_2"].mean() #split the data samples in living with their parents and moved out for 2019 and calculate means grouped2 = df_2.groupby(df_2.living) Moved_out_2020 = grouped2.get_group("Moved_out") mean3 = Moved_out_2020["attitu_2"].mean() Parents_2020 = grouped2.get_group("Living_with_parents") mean4 = Parents_2020["attitu_2"].mean() #plot in a figure the perception values
MYCATEGORICAL = set() for name, mytype in zip(data.dtypes.index, data.dtypes.values): if mytype == object: MYCATEGORICAL.add(name) for c in categorical: MYCATEGORICAL.add(c) MYCATEGORICAL = list(MYCATEGORICAL) if len(MYCATEGORICAL) > 0 else None # create grouped_table with p values # API: https://tableone.readthedocs.io/en/latest/tableone.html # Example: https://github.com/tompollard/tableone/blob/master/tableone.ipynb grouped_table = TableOne(data, categorical=MYCATEGORICAL, groupby=group, nonnormal=nonnormal, label_suffix=True, pval=pval) print(grouped_table) if outfile: if outFMT == 'csv': grouped_table.to_csv(outfile) elif outFMT == 'latex': grouped_table.to_latex(outfile) elif outFMT == 'html': grouped_table.to_html(outfile) sys.stdout.flush() sys.stdout.close() sys.stderr.flush() sys.stderr.close()