def qdata_flatten(data, code, quesid=None, userid_begin=None):
    '将问卷数据展平，字段如下\n    userid: 用户ID\n    quesid: 问卷ID\n    qnum: 题号\n    qname: 题目内容\n    qtype: 题目类型\n    samplelen:题目的样本数\n    itemnum: 选项序号\n    itemname: 选项内容\n    code: 用户的选择\n    codename: 用户选择的具体值\n    count: 计数\n    percent(%): 计数占比（百分比）\n    '
    if (not userid_begin):
        userid_begin = 1000000
    data.index = [((userid_begin + i) + 1) for i in range(len(data))]
    if ('提交答卷时间' in data.columns):
        begin_date = pd.to_datetime(data['提交答卷时间']).min().strftime('%Y-%m-%d')
        end_date = pd.to_datetime(data['提交答卷时间']).max().strftime('%Y-%m-%d')
    else:
        begin_date = ''
        end_date = ''
    (data, code) = to_dummpy(data, code, qtype_new='单选题')
    code_item = {}
    for qq in code:
        if (code[qq]['qtype'] == '矩阵单选题'):
            code_item.update(code[qq]['code_r'])
        else:
            code_item.update(code[qq]['code'])
    qdata = data.stack().reset_index()
    qdata.columns = ['userid', 'qn_an', 'code']
    qdata['qnum'] = qdata['qn_an'].map((lambda x: x.split('_')[0]))
    qdata['itemnum'] = qdata['qn_an'].map((lambda x: '_'.join(x.split('_')[1:])))
    if quesid:
        qdata['quesid'] = quesid
        qdata = qdata[['userid', 'quesid', 'qnum', 'itemnum', 'code']]
    else:
        qdata = qdata[['userid', 'qnum', 'itemnum', 'code']]
    samplelen = qdata.groupby(['userid', 'qnum'])['code'].sum().map((lambda x: int((x > 0)))).unstack().sum()
    quesinfo = qdata.groupby(['qnum', 'itemnum', 'code'])['code'].count()
    quesinfo.name = 'count'
    quesinfo = quesinfo.reset_index()
    quesinfo = quesinfo[(quesinfo['code'] != 0)]
    quesinfo['samplelen'] = quesinfo['qnum'].replace(samplelen.to_dict())
    quesinfo['percent(%)'] = 0
    quesinfo.loc[((quesinfo['samplelen'] > 0), 'percent(%)')] = ((100 * quesinfo.loc[((quesinfo['samplelen'] > 0), 'count')]) / quesinfo.loc[((quesinfo['samplelen'] > 0), 'samplelen')])
    quesinfo['qname'] = quesinfo['qnum'].map((lambda x: code[x]['content']))
    quesinfo['qtype'] = quesinfo['qnum'].map((lambda x: code[x]['qtype']))
    quesinfo['itemname'] = (quesinfo['qnum'] + quesinfo['itemnum'].map((lambda x: ('_%s' % x))))
    quesinfo['itemname'] = quesinfo['itemname'].replace(code_item)
    quesinfo['codename'] = ''
    quesinfo.loc[((quesinfo['code'] == 0), 'codename')] = '否'
    quesinfo.loc[((quesinfo['code'] == 1), 'codename')] = '是'
    quesinfo['tmp'] = (quesinfo['qnum'] + quesinfo['code'].map((lambda x: ('_%s' % int(x)))))
    quesinfo['codename'].update(quesinfo.loc[(((quesinfo['code'] > 0) & (quesinfo['qtype'] == '矩阵单选题')), 'tmp')].map((lambda x: code[x.split('_')[0]]['code'][int(x.split('_')[1])])))
    quesinfo['codename'].update(quesinfo.loc[(((quesinfo['code'] > 0) & (quesinfo['qtype'] == '排序题')), 'tmp')].map((lambda x: 'Top{}'.format(x.split('_')[1]))))
    quesinfo['begin_date'] = begin_date
    quesinfo['end_date'] = end_date
    if quesid:
        quesinfo['quesid'] = quesid
        quesinfo = quesinfo[['quesid', 'begin_date', 'end_date', 'qnum', 'qname', 'qtype', 'samplelen', 'itemnum', 'itemname', 'code', 'codename', 'count', 'percent(%)']]
    else:
        quesinfo = quesinfo[['qnum', 'qname', 'qtype', 'samplelen', 'itemnum', 'itemname', 'code', 'codename', 'count', 'percent(%)']]
    quesinfo['qnum'] = quesinfo['qnum'].astype('category')
    quesinfo['qnum'].cat.set_categories(sorted(list(quesinfo['qnum'].unique()), key=(lambda x: int(re.findall('\\d+', x)[0]))), inplace=True)
    quesinfo['itemnum'] = quesinfo['itemnum'].astype('category')
    quesinfo['itemnum'].cat.set_categories(sorted(list(quesinfo['itemnum'].unique()), key=(lambda x: int(re.findall('\\d+', x)[0]))), inplace=True)
    quesinfo = quesinfo.sort_values(['qnum', 'itemnum', 'code'])
    return (qdata, quesinfo)
将问卷数据展平，字段如下
userid: 用户ID
quesid: 问卷ID
qnum: 题号
qname: 题目内容
qtype: 题目类型
samplelen:题目的样本数
itemnum: 选项序号
itemname: 选项内容
code: 用户的选择
codename: 用户选择的具体值
count: 计数
percent(%): 计数占比（百分比）
def qdata_flatten(data, code, quesid=None, userid_begin=None):
    '将问卷数据展平，字段如下\n    userid: 用户ID\n    quesid: 问卷ID\n    qnum: 题号\n    qname: 题目内容\n    qtype: 题目类型\n    samplelen:题目的样本数\n    itemnum: 选项序号\n    itemname: 选项内容\n    code: 用户的选择\n    codename: 用户选择的具体值\n    count: 计数\n    percent(%): 计数占比（百分比）\n    '
    if (not userid_begin):
        userid_begin = 1000000
    data.index = [((userid_begin + i) + 1) for i in range(len(data))]
    if ('提交答卷时间' in data.columns):
        begin_date = pd.to_datetime(data['提交答卷时间']).min().strftime('%Y-%m-%d')
        end_date = pd.to_datetime(data['提交答卷时间']).max().strftime('%Y-%m-%d')
    else:
        begin_date = 
        end_date = 
    (data, code) = to_dummpy(data, code, qtype_new='单选题')
    code_item = {}
    for qq in code:
        if (code[qq]['qtype'] == '矩阵单选题'):
            code_item.update(code[qq]['code_r'])
        else:
            code_item.update(code[qq]['code'])
    qdata = data.stack().reset_index()
    qdata.columns = ['userid', 'qn_an', 'code']
    qdata['qnum'] = qdata['qn_an'].map((lambda x: x.split('_')[0]))
    qdata['itemnum'] = qdata['qn_an'].map((lambda x: '_'.join(x.split('_')[1:])))
    if quesid:
        qdata['quesid'] = quesid
        qdata = qdata[['userid', 'quesid', 'qnum', 'itemnum', 'code']]
    else:
        qdata = qdata[['userid', 'qnum', 'itemnum', 'code']]
    samplelen = qdata.groupby(['userid', 'qnum'])['code'].sum().map((lambda x: int((x > 0)))).unstack().sum()
    quesinfo = qdata.groupby(['qnum', 'itemnum', 'code'])['code'].count()
    quesinfo.name = 'count'
    quesinfo = quesinfo.reset_index()
    quesinfo = quesinfo[(quesinfo['code'] != 0)]
    quesinfo['samplelen'] = quesinfo['qnum'].replace(samplelen.to_dict())
    quesinfo['percent(%)'] = 0
    quesinfo.loc[((quesinfo['samplelen'] > 0), 'percent(%)')] = ((100 * quesinfo.loc[((quesinfo['samplelen'] > 0), 'count')]) / quesinfo.loc[((quesinfo['samplelen'] > 0), 'samplelen')])
    quesinfo['qname'] = quesinfo['qnum'].map((lambda x: code[x]['content']))
    quesinfo['qtype'] = quesinfo['qnum'].map((lambda x: code[x]['qtype']))
    quesinfo['itemname'] = (quesinfo['qnum'] + quesinfo['itemnum'].map((lambda x: ('_%s' % x))))
    quesinfo['itemname'] = quesinfo['itemname'].replace(code_item)
    quesinfo['codename'] = 
    quesinfo.loc[((quesinfo['code'] == 0), 'codename')] = '否'
    quesinfo.loc[((quesinfo['code'] == 1), 'codename')] = '是'
    quesinfo['tmp'] = (quesinfo['qnum'] + quesinfo['code'].map((lambda x: ('_%s' % int(x)))))
    quesinfo['codename'].update(quesinfo.loc[(((quesinfo['code'] > 0) & (quesinfo['qtype'] == '矩阵单选题')), 'tmp')].map((lambda x: code[x.split('_')[0]]['code'][int(x.split('_')[1])])))
    quesinfo['codename'].update(quesinfo.loc[(((quesinfo['code'] > 0) & (quesinfo['qtype'] == '排序题')), 'tmp')].map((lambda x: 'Top{}'.format(x.split('_')[1]))))
    quesinfo['begin_date'] = begin_date
    quesinfo['end_date'] = end_date
    if quesid:
        quesinfo['quesid'] = quesid
        quesinfo = quesinfo[['quesid', 'begin_date', 'end_date', 'qnum', 'qname', 'qtype', 'samplelen', 'itemnum', 'itemname', 'code', 'codename', 'count', 'percent(%)']]
    else:
        quesinfo = quesinfo[['qnum', 'qname', 'qtype', 'samplelen', 'itemnum', 'itemname', 'code', 'codename', 'count', 'percent(%)']]
    quesinfo['qnum'] = quesinfo['qnum'].astype('category')
    quesinfo['qnum'].cat.set_categories(sorted(list(quesinfo['qnum'].unique()), key=(lambda x: int(re.findall('\\d+', x)[0]))), inplace=True)
    quesinfo['itemnum'] = quesinfo['itemnum'].astype('category')
    quesinfo['itemnum'].cat.set_categories(sorted(list(quesinfo['itemnum'].unique()), key=(lambda x: int(re.findall('\\d+', x)[0]))), inplace=True)
    quesinfo = quesinfo.sort_values(['qnum', 'itemnum', 'code'])
    return (qdata, quesinfo)