python-3.x - 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
解决方案
推荐阅读
- java - 在命令行参数中为字符获取不同的输出
- c# - 在将集合引用从视图传递到 VM 时,跟踪集合引用的最佳方法是什么
- javascript - 自定义 Javascript 变量在历史更改时返回不同的值
- python - 在 Python Pandas 中对行(日期)进行分组并汇总多个列(每个日期的几个测量值)
- c++ - boost::spirit::x3 phrase_parse 在推入向量之前进行算术运算
- unit-testing - 如何在 Rust 中断言 io 错误?
- asp.net-core - 具有多个注册的 IAuthorizationHandler - 依赖解析器如何选择正确的实现?
- api - 如何在烧瓶 api 服务 (SPA) 中信任 azure 活动目录不记名令牌
- android - 如何在 Android Studio 中创建带有渐变动画的三星“立即修复”按钮
- c# - 为什么 Unity (DI) 可以在 Controller 中工作,而不能在我的服务层中工作?