python - 使用 for 循环有没有办法控制哪个 # 循环将值附加到列表中?
问题描述
我目前正在使用 3 个名为 的数据框,doctorate
看起来有点像这样:high_school
bachelor
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 循环以便能够做到这一点,但我无法弥合到达那里的差距。有人可以帮我吗?
解决方案
在这种情况下,您尝试通过键/字符串查找列表。为什么不只使用列表的字典?
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)
推荐阅读
- mysql - 无法添加主键约束
- python - 网页抓取 - 进入第 2 页
- javascript - 在一个承诺中,我想将值(值本身来自另一个承诺)分配给一个将在该承诺之外使用的变量
- javascript - 如何制作垂直进度指示图
- python - 将函数作为参数传递给另一个函数时如何指定输入函数参数类型和返回类型?
- python - dtype 'object' 的 4D np.array 与 dtype 'uint8' 的 3D np.array 作为元素
- json - 使用 Gspread 逐行更新 Google 电子表格
- r - 在循环中如何更改具有特定名称的目录?
- swift - 无法从 VC 转到另一个 VC,不知道为什么
- google-apps-script - jquery mobile折叠方法不起作用