首页 > 解决方案 > python中的特征因子表

问题描述

我有数据框,我有列 StudentInquiryCategory studentProgram 和 Enrollment,enrollment 的值为 0 和 1,StudentInquirCategory 有 Inquiry 源,StudentProgram 有 Courses 我想创建这样的因子表

学生查询类别 搜索 网站 场地 电子邮件/社交
学生程序类型
医学博士 0.25 0.36 0.21 0.11
非学位 0.66 0.22 0.35 0.79
证书 0.45 0.21 0.58 0.32
ED 0.75 0.65 0.28 0.97

#代码

ProgramEnRate = []

InqcatEnRate = []

data_dict = dict()

df_fact = pd.DataFrame()

def Program_Shape(Program, Category):

    for p in Program:
        ProgramtypeEn = df1[(df1["ProgramType"] == (p)) & (df1["Enrolled"] == 1)]
        Programtype = df1[df1["ProgramType"] == (p)]
        PrEn = ProgramtypeEn.shape[0]
        PrType = Programtype.shape[0]
        if PrEn != 0:
            res = (PrEn/PrType)
        else:
            res = 0        
        for c in Category:
            Inqcat = ProgramtypeEn[ProgramtypeEn["InquiryCategory"] == (c)]
            if Inqcat.shape[0] != 0:
                res1 = Inqcat.shape[0]/PrEn
            else:
                res1 = 0
            InqcatEnRate.append(res1)
            if (res1 != 0) or (res != 0):
                InqCatres = res1/res
            else:
                InqCatres = 0
        ProgramEnRate.append(res)
    print("ProgramType: ", ProgramEnRate)
    print("InquiryCategory: ",InqcatEnRate)
Program_Shape(rows, cols)

'''
#cols
array(['cpc', 'direct', 'manual-entry', 'paid-social', 'email',
       'referral', 'field', nan, 'display', 'Partner', 'Website',
       'social', 'Search', 'organic', 'Email/Social/Alum', 'test', 'Test',
       'Media', 'historical', 'Other', 'Aggregate', 'Affiliate', 'ESA',
       'video', 'website'], dtype=object)

#rows
array(['Masters of Education (M.Ed.)', 'Non-Degree', 'Certificate',
       'Doctorate - Ed.D.', nan, 'Bachelor of Science - B.S.',
       'Masters of Arts (M.A.)', 'Master of Science in Nursing (MSN)',
       'Doctorate - Ed.S.', 'Transition to Teaching (T2T)', 'Doctorate'],
      dtype=object)
I have created code but no success

                
                

标签: python-3.xpandasdataframemachine-learningdata-science

解决方案


推荐阅读