0

I guess I am running into a beginner problem: -> I want to loop over an array and insert the the values into lines of code that are executed. For the attempt below I get "SyntaxError: can't assign to operator"

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

Thanks for helping!

My expected output would be:

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"]
7
  • insert the the values into lines of code that are executed What do you mean? - Also what is your expected output? Commented Jun 26, 2019 at 12:16
  • What does it mean to 'insert values into lines of code that are executed'? Can you show what your expected output is>? Commented Jun 26, 2019 at 12:16
  • 1
    @B001ᛦ Lol, I love how we have an identical comment. Commented Jun 26, 2019 at 12:17
  • You need to store the result s.lower() + '_immu_edu' somewhere as it is a temporary (also known as an "rvalue" in some languages). You can't assign to it. Commented Jun 26, 2019 at 12:17
  • 1
    Possible duplicate of How do I create a variable number of variables? Commented Jun 26, 2019 at 12:29

1 Answer 1

0

Store your values in a dictionary and access using the keys:

my_dict = dict()
for s in country_filter:  
  my_dict[s.lower() + '_immu_edu'] = immu_edu.loc[immu_edu['CountryName'] == s]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.