For loop error for Exploring Data with pandas: Intermediate step 11

I am currently doing this exercise and I am stuck.

I would like to know why my for loop only returns the row for “Israel”. I was able to get it to work earlier to iterate over the entire countries array using my own code. My dictionary values were incorrect so I re-wrote the code and this is what’s been happening ever since. I tried using the exact same code in the exercise but I’m still getting only “Israel”. Is there something I’m doing wrong?

My Code:

top_employer_by_country = {}
countries = f500["country"].unique()
for c in countries:
    selected_rows = f500[f500["country"] == c].sort_values("employees",ascending=False)

What I expected to happen: This should iterate over the entire countries array.

What actually happened: It only returns the value for “Israel” when I use the variable inspector. See image attached.

Your code does iterate over the entire countries array.

That’s because your are updating selected_rows at each iteration of the loop.

If you have a simple list -

a = [1, 2, 3, 4]

And you iterate over it -

for item in a:
    b = item

For every iteration b will be updated with the value of item. So, once all the iterations are completed, b will end up with the last item in your list. Which would be 4. All previous values, 1, 2, 3, get overwritten as the loop progresses.

That’s why you are required to store the results in a dictionary. Because in a dictionary, you end up assigning the value at each iteration to a specific key. And unless you use the same key for any particular iteration, each key would have a different value assigned to it.

That’s what they do in the exercise in the content as well -

# Use a for loop to iterate over the countries
for c in countries:
    # Use boolean comparison to select only rows that
    # correspond to a specific country
    selected_rows = f500[f500["country"] == c]
    # Calculate the mean average revenue for just those rows
    mean = selected_rows["revenues"].mean()
    # Assign the mean value to the dictionary, using the
    # country name as the key
    avg_rev_by_country[c] = mean

They assign the mean to the dictionary avg_rev_by_country with the keys based on c.

You already have an empty dictionary assigned there with top_employer_by_country. You have to incorporate it into your for loop as per the instructions.

1 Like

I understand it now based on what you said. I was getting that one value because I was trying to check if my for loop was working as desired in the exercise. I didn’t know that for loops behaved this way and have to be written completely up until the assignment part to work correctly.

I wrote my for loop completely this time and it worked.

top_employer_by_country = {}
countries = f500["country"].unique()
for c in countries:
    selected_rows = f500[f500["country"] == c].sort_values("employees",ascending=False)
    first_row = selected_rows.iloc[0]
    first_company = first_row[0]
    top_employer_by_country[c] = first_company
1 Like