我想将diff函数应用于数据帧中的每一行,基于它所属的类别
def weigh_by_education(row):
if education_income.education_level == 'College':
return row / 1013
if education_income.education_level == 'Doctorate':
return row / 451
if education_income.education_level == 'Graduate':
return row / 3128
if education_income.education_level == 'High School':
return row / 2013
if education_income.education_level == 'Post-Graduate':
return row / 516
if education_income.education_level == 'Uneducated':
return row / 1487
else:
return row / 1519
这是我的职责我将其应用于我的数据框架,尝试创建一个名为%s->;的新列;每个教育级别中的用户数量由相应类别中的总人数加权
education_income['percent'] = education_income['user_id'].apply(lambda row: weigh_by_education(row))
但是每次它抛出一个ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我的原始数据框架按列分组:教育水平、收入类别。“值”列中是用户计数。我想通过每个教育级别类别的总人数来衡量用户数量。我能做什么
您可能需要使用
numpy.select
相关问题 更多 >
编程相关推荐