首页 > 解决方案 > 使用 for 循环有没有办法控制哪个 # 循环将值附加到列表中?

问题描述

我目前正在使用 3 个名为 的数据框,doctorate看起来有点像这样:high_schoolbachelor

    ID  age education   marital_status  occupation  annual_income   Age_25  Age_30  Age_35  Age_40  Age_45  Age_50
1   2   50  doctorate   married professional    mid 25 and over 30 and over 35 and over 40 and over 45 and over 50 and over
7   8   40  doctorate   married professional    high    25 and over 30 and over 35 and over 40 and over under 45    under 50
11  12  45  doctorate   married professional    mid 25 and over 30 and over 35 and over 40 and over 45 and over under 50
16  17  44  doctorate   divorced    transport   mid 25 and over 30 and over 35 and over 40 and over under 45    under 50

annual_income我正在尝试使用以下 for 循环根据列创建概率:

income_levels = ['low','mid','high']
education_levels = [bachelor,doctorate,high_school]

for inc_level in income_levels:
    for ed_level in education_levels:
        print(inc_level,len(ed_level[ed_level['annual_income'] == inc_level]) / len(ed_level))

它产生了这个,这就是我想要的:

low 0.125
low 0.0
low 0.25
mid 0.625
mid 0.75
mid 0.5
high 0.25
high 0.25
high 0.25

但是,我希望能够根据收入类别将这些值附加到列表中,列表将是low_income, mid_income, high_income。我确信有一种方法可以修改我的 for 循环以便能够做到这一点,但我无法弥合到达那里的差距。有人可以帮我吗?

标签: pythonpandaslistfor-loop

解决方案


在这种情况下,您尝试通过键/字符串查找列表。为什么不只使用列表的字典?

income_levels = ['low','mid','high']
education_levels = [bachelor,doctorate,high_school]

# initial dictionary
inc_level_rates = {il: list() for il in income_levels}

for inc_level in income_levels:
    for ed_level in education_levels:
        rate = len(ed_level[ed_level['annual_income'] == inc_level]) / len(ed_level)
        inc_level_rates[inc_level].append(rate)
        print(inc_level, rate)

print(inc_level_rates)

推荐阅读