Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用for循环是否有方法控制哪个#循环将值附加到列表?_Python_Pandas_List_For Loop - Fatal编程技术网

Python 使用for循环是否有方法控制哪个#循环将值附加到列表?

Python 使用for循环是否有方法控制哪个#循环将值附加到列表?,python,pandas,list,for-loop,Python,Pandas,List,For Loop,我目前正在使用3个数据帧,分别命名为博士、高中和学士,看起来有点像这样: 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 ove

我目前正在使用3个数据帧,分别命名为
博士
高中
学士
,看起来有点像这样:

    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
我试图使用以下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


但是,我希望能够根据收入类别将这些值附加到列表中,列表将是
低收入
中等收入
高收入
。我确信有一种方法可以修改for循环以实现这一点,但我无法弥合实现这一点的差距。有人能帮我吗?

在这种情况下,您试图通过键/字符串查找列表。为什么不直接使用一份清单呢

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)

在本例中,您试图通过键/字符串查找列表。为什么不直接使用一份清单呢

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)

似乎组合3个数据帧然后使用groupby是有意义的,如中所示,这样可以保留标签和顺序,您可以使用tolist或todict。似乎组合3个数据帧然后使用groupby是有意义的,如中所示,这样可以保留标签和顺序,您可以使用tolist或todict