对数据帧中的不同行应用不同的函数

2024-04-27 15:50:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将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().

我的原始数据框架按列分组:教育水平、收入类别。“值”列中是用户计数。我想通过每个教育级别类别的总人数来衡量用户数量。我能做什么


Tags: 数据框架byreturnif级别类别level
1条回答
网友
1楼 · 发布于 2024-04-27 15:50:03

您可能需要使用numpy.select

conditions = [
    df.education_level == 'College',
    df.education_level == 'Doctorate'
]

values = [
    df.some_column / 1013,
    df.some_column / 451
]

df['percent'] = np.select(conditions, values)

相关问题 更多 >