Python-For循环-运行输出的每一行

时间:2019-06-26 12:14:57

标签: python for-loop

我想我遇到了一个初学者的问题: ->我想遍历一个数组,然后将值插入执行的代码行中。 对于下面的尝试,我得到“ SyntaxError:无法分配给运算符”

#Country-subsets (all countries in dataframe)
for s in country_filter:  
  s.lower() + '_immu_edu' = immu_edu.loc[immu_edu['CountryName'] == s]

感谢您的帮助!

我的预期输出是:

guinea_immu_edu = immu_edu.loc[immu_edu['CountryName'] == "Guinea"]
lao_immu_edu = immu_edu.loc[immu_edu['CountryName'] == "Lao PDR"]
bf_immu_edu = immu_edu.loc[immu_edu['CountryName'] == "Burkina Faso"]
us_immu_edu = immu_edu.loc[immu_edu['CountryName'] == "United States"]
ge_immu_edu = immu_edu.loc[immu_edu['CountryName'] == "Germany"]

1 个答案:

答案 0 :(得分:0)

将值存储在字典中,并使用以下键进行访问:

my_dict = dict()
for s in country_filter:  
  my_dict[s.lower() + '_immu_edu'] = immu_edu.loc[immu_edu['CountryName'] == s]