def table(data, code, total=True):
    "\n    单个题目描述统计\n    code是data的编码，列数大于1\n    返回字典格式数据：\n    'fop'：百分比, 对于单选题和为1，多选题分母为样本数\n    'fo'： 观察频数表，其中添加了合计项\n    'fw':  加权频数表，可实现平均值、T2B等功能，仅当code中存在关键词'weight'时才有\n    "
    qtype = code['qtype']
    index = code['qlist']
    data = pd.DataFrame(data)
    sample_len = data[code['qlist']].notnull().T.any().sum()
    result = {}
    if (qtype == u'单选题'):
        fo = data.iloc[:, 0].value_counts()
        if ('weight' in code):
            w = pd.Series(code['weight'])
            fo1 = fo[w.index][fo[w.index].notnull()]
            fw = ((fo1 * w).sum() / fo1.sum())
            result['fw'] = fw
        fo.sort_values(ascending=False, inplace=True)
        fop = fo.copy()
        fop = ((fop / fop.sum()) * 1.0)
        fop[u'合计'] = fop.sum()
        fo[u'合计'] = fo.sum()
        if ('code' in code):
            fop.rename(index=code['code'], inplace=True)
            fo.rename(index=code['code'], inplace=True)
        fop.name = u'占比'
        fo.name = u'频数'
        fop = pd.DataFrame(fop)
        fo = pd.DataFrame(fo)
        result['fo'] = fo
        result['fop'] = fop
    elif (qtype == u'多选题'):
        fo = data.sum()
        fo.sort_values(ascending=False, inplace=True)
        fo[u'合计'] = fo.sum()
        if ('code' in code):
            fo.rename(index=code['code'], inplace=True)
        fop = fo.copy()
        fop = (fop / sample_len)
        fop.name = u'占比'
        fo.name = u'频数'
        fop = pd.DataFrame(fop)
        fo = pd.DataFrame(fo)
        result['fop'] = fop
        result['fo'] = fo
    elif (qtype == u'矩阵单选题'):
        fo = pd.DataFrame(columns=code['qlist'], index=sorted(code['code']))
        for i in fo.columns:
            fo.loc[:, i] = data[i].value_counts()
        if ('weight' not in code):
            code['weight'] = dict(zip(code['code'].keys(), code['code'].keys()))
        fw = pd.DataFrame(columns=[u'加权'], index=code['qlist'])
        w = pd.Series(code['weight'])
        for c in fo.columns:
            t = fo[c]
            t = t[w.index][t[w.index].notnull()]
            if (t.sum() > 1e-17):
                fw.loc[(c, u'加权')] = ((t * w).sum() / t.sum())
            else:
                fw.loc[(c, u'加权')] = 0
        fw.rename(index=code['code_r'], inplace=True)
        result['fw'] = fw
        result['weight'] = ','.join(['{}:{}'.format(code['code'][c], code['weight'][c]) for c in code['code']])
        fo.rename(columns=code['code_r'], index=code['code'], inplace=True)
        fop = fo.copy()
        fop = (fop / sample_len)
        result['fop'] = fop
        result['fo'] = fo
    elif (qtype == u'排序题'):
        topn = data[index].fillna(0).max().max()
        topn = int(topn)
        qsort = dict(zip([(i + 1) for i in range(topn)], [((((topn - i) * 2.0) / (topn + 1)) / topn) for i in range(topn)]))
        top1 = data.applymap((lambda x: int((x == 1))))
        data_weight = data.replace(qsort)
        t1 = pd.DataFrame()
        t1['TOP1'] = top1.sum()
        t1[u'综合'] = data_weight.sum()
        t1.sort_values(by=u'综合', ascending=False, inplace=True)
        t1.rename(index=code['code'], inplace=True)
        t = t1.copy()
        t = (t / sample_len)
        result['fop'] = t
        result['fo'] = t1
        t_topn = pd.DataFrame()
        for i in range(topn):
            t_topn[('TOP%d' % (i + 1))] = data.applymap((lambda x: int((x == (i + 1))))).sum()
        t_topn.sort_values(by=u'TOP1', ascending=False, inplace=True)
        if ('code' in code):
            t_topn.rename(index=code['code'], inplace=True)
        result['TOPN_fo'] = t_topn
        result['TOPN'] = (t_topn / sample_len)
        result['weight'] = '+'.join(['TOP{}*{:.2f}'.format((i + 1), ((((topn - i) * 2.0) / (topn + 1)) / topn)) for i in range(topn)])
    else:
        result['fop'] = None
        result['fo'] = None
    if ((not total) and (not (result['fo'] is None)) and (u'合计' in result['fo'].index)):
        result['fo'].drop([u'合计'], axis=0, inplace=True)
        result['fop'].drop([u'合计'], axis=0, inplace=True)
    if ((not (result['fo'] is None)) and ('code_order' in code)):
        code_order = [q for q in code['code_order'] if (q in result['fo'].index)]
        if (u'合计' in result['fo'].index):
            code_order = (code_order + [u'合计'])
        result['fo'] = pd.DataFrame(result['fo'], index=code_order)
        result['fop'] = pd.DataFrame(result['fop'], index=code_order)
    return result
单个题目描述统计
code是data的编码，列数大于1
返回字典格式数据：
'fop'：百分比, 对于单选题和为1，多选题分母为样本数
'fo'： 观察频数表，其中添加了合计项
'fw':  加权频数表，可实现平均值、T2B等功能，仅当code中存在关键词'weight'时才有
def table(data, code, total=True):
    "\n    单个题目描述统计\n    code是data的编码，列数大于1\n    返回字典格式数据：\n    'fop'：百分比, 对于单选题和为1，多选题分母为样本数\n    'fo'： 观察频数表，其中添加了合计项\n    'fw':  加权频数表，可实现平均值、T2B等功能，仅当code中存在关键词'weight'时才有\n    "
    qtype = code['qtype']
    index = code['qlist']
    data = pd.DataFrame(data)
    sample_len = data[code['qlist']].notnull().T.any().sum()
    result = {}
    if (qtype == u'单选题'):
        fo = data.iloc[:, 0].value_counts()
        if ('weight' in code):
            w = pd.Series(code['weight'])
            fo1 = fo[w.index][fo[w.index].notnull()]
            fw = ((fo1 * w).sum() / fo1.sum())
            result['fw'] = fw
        fo.sort_values(ascending=False, inplace=True)
        fop = fo.copy()
        fop = ((fop / fop.sum()) * 1.0)
        fop[u'合计'] = fop.sum()
        fo[u'合计'] = fo.sum()
        if ('code' in code):
            fop.rename(index=code['code'], inplace=True)
            fo.rename(index=code['code'], inplace=True)
        fop.name = u'占比'
        fo.name = u'频数'
        fop = pd.DataFrame(fop)
        fo = pd.DataFrame(fo)
        result['fo'] = fo
        result['fop'] = fop
    elif (qtype == u'多选题'):
        fo = data.sum()
        fo.sort_values(ascending=False, inplace=True)
        fo[u'合计'] = fo.sum()
        if ('code' in code):
            fo.rename(index=code['code'], inplace=True)
        fop = fo.copy()
        fop = (fop / sample_len)
        fop.name = u'占比'
        fo.name = u'频数'
        fop = pd.DataFrame(fop)
        fo = pd.DataFrame(fo)
        result['fop'] = fop
        result['fo'] = fo
    elif (qtype == u'矩阵单选题'):
        fo = pd.DataFrame(columns=code['qlist'], index=sorted(code['code']))
        for i in fo.columns:
            fo.loc[:, i] = data[i].value_counts()
        if ('weight' not in code):
            code['weight'] = dict(zip(code['code'].keys(), code['code'].keys()))
        fw = pd.DataFrame(columns=[u'加权'], index=code['qlist'])
        w = pd.Series(code['weight'])
        for c in fo.columns:
            t = fo[c]
            t = t[w.index][t[w.index].notnull()]
            if (t.sum() > 1e-17):
                fw.loc[(c, u'加权')] = ((t * w).sum() / t.sum())
            else:
                fw.loc[(c, u'加权')] = 0
        fw.rename(index=code['code_r'], inplace=True)
        result['fw'] = fw
        result['weight'] = ','.join(['{}:{}'.format(code['code'][c], code['weight'][c]) for c in code['code']])
        fo.rename(columns=code['code_r'], index=code['code'], inplace=True)
        fop = fo.copy()
        fop = (fop / sample_len)
        result['fop'] = fop
        result['fo'] = fo
    elif (qtype == u'排序题'):
        topn = data[index].fillna(0).max().max()
        topn = int(topn)
        qsort = dict(zip([(i + 1) for i in range(topn)], [((((topn - i) * 2.0) / (topn + 1)) / topn) for i in range(topn)]))
        top1 = data.applymap((lambda x: int((x == 1))))
        data_weight = data.replace(qsort)
        t1 = pd.DataFrame()
        t1['TOP1'] = top1.sum()
        t1[u'综合'] = data_weight.sum()
        t1.sort_values(by=u'综合', ascending=False, inplace=True)
        t1.rename(index=code['code'], inplace=True)
        t = t1.copy()
        t = (t / sample_len)
        result['fop'] = t
        result['fo'] = t1
        t_topn = pd.DataFrame()
        for i in range(topn):
            t_topn[('TOP%d' % (i + 1))] = data.applymap((lambda x: int((x == (i + 1))))).sum()
        t_topn.sort_values(by=u'TOP1', ascending=False, inplace=True)
        if ('code' in code):
            t_topn.rename(index=code['code'], inplace=True)
        result['TOPN_fo'] = t_topn
        result['TOPN'] = (t_topn / sample_len)
        result['weight'] = '+'.join(['TOP{}*{:.2f}'.format((i + 1), ((((topn - i) * 2.0) / (topn + 1)) / topn)) for i in range(topn)])
    else:
        result['fop'] = None
        result['fo'] = None
    if ((not total) and (not (result['fo'] is None)) and (u'合计' in result['fo'].index)):
        result['fo'].drop([u'合计'], axis=0, inplace=True)
        result['fop'].drop([u'合计'], axis=0, inplace=True)
    if ((not (result['fo'] is None)) and ('code_order' in code)):
        code_order = [q for q in code['code_order'] if (q in result['fo'].index)]
        if (u'合计' in result['fo'].index):
            code_order = (code_order + [u'合计'])
        result['fo'] = pd.DataFrame(result['fo'], index=code_order)
        result['fop'] = pd.DataFrame(result['fop'], index=code_order)
    return result