What is wrong with my pandas' loop?

Screen Link:

My Code:

top_employer_by_country = {}

countries = f500["country"].unique()

for c in countries:
    row_top_employer = f500[f500["country"] == c].sort_values("employees", ascending=False).head(1)
    top_employer = row_top_employer.loc[0, "company"]
    top_employer_by_country[c] = top_employer

What I expected to happen:
The top_employer_by_country should be populated.

What actually happened:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-1-d42274fd524b> in <module>
     12 for c in countries:
     13     row_top_employer = f500[f500["country"] == c].sort_values("employees", ascending=False).head(1)
---> 14     top_employer = row_top_employer.loc[0, "company"]
     15     top_employer_by_country[c] = top_employer

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1760                 except (KeyError, IndexError, AttributeError):
   1761                     pass
-> 1762             return self._getitem_tuple(key)
   1763         else:
   1764             # we by definition only have the 0th axis

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1270     def _getitem_tuple(self, tup: Tuple):
   1271         try:
-> 1272             return self._getitem_lowerdim(tup)
   1273         except IndexingError:
   1274             pass

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_lowerdim(self, tup)
   1387         for i, key in enumerate(tup):
   1388             if is_label_like(key) or isinstance(key, tuple):
-> 1389                 section = self._getitem_axis(key, axis=i)
   1390 
   1391                 # we have yielded a scalar ?

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1963         # fall thru to straight lookup
   1964         self._validate_key(key, axis)
-> 1965         return self._get_label(key, axis=axis)
   1966 
   1967 

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexing.py in _get_label(self, label, axis)
    623             raise IndexingError("no slices here, handle elsewhere")
    624 
--> 625         return self.obj._xs(label, axis=axis)
    626 
    627     def _get_loc(self, key: int, axis: int):

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/generic.py in xs(self, key, axis, level, drop_level)
   3535             loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
   3536         else:
-> 3537             loc = self.index.get_loc(key)
   3538 
   3539             if isinstance(loc, np.ndarray):

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

If I run the code as follows:

top_employer_by_country = {}

countries = f500["country"].unique()

for c in countries:
    row_top_employer = f500[f500["country"] == c].sort_values("employees", ascending=False).head(1)
    top_employer = row_top_employer.loc[0, "company"]
    top_employer_by_country[c] = top_employer
**break**

We can see that the top_employer_by_country is populated with one data:
“USA”: “Walmart.”

Why is this happening?

Please include the link to the screen in which this happens in your post.

What follows is the same code as before (except for that odd **break** thing which I don’t know what it means.

When I am not using break:

When I am using Break:

:point_up_2::point_up_2: