import re group_list = 'NS:FS:CS'.split(':') indice = 'Age:BMI'.split(':') out = open('Final_Result.NS-FS-CS.mixcon.xls', 'w') dt = pd.read_table('Result.NS-FS-CS.mixcon.xls', sep = '\t', index_col = 0) print('Characteristic\t' + '\t'.join(group_list) + '\tP value', file = out) for x in indice: sub_dt = dt.loc[x] line = '' for group in group_list: Mean = 'mean('+ group + ')' Sd = 'sd('+ group + ')' for colname in sub_dt.index: if colname == Mean: group_mean = str(round(sub_dt[colname],2)) line = line + '\t'+ group_mean if colname == Sd: group_sd = '('+str(round(sub_dt[colname],2))+')' line = line + group_sd print(x +',mean(sd)' + line + '\t%.2e' % dt.loc[x]['pvalue'], file = out) #科学计数法 out.close()