My Code
for s in unique_sector:
sector_bool = f500["sector"] == s
sector_df = f500[sector_bool]
sector_df = sector_df.sort_values("roa", ascending = False)
first_row = sector_df.head(1)
top_roa = first_row["company"]
top_roa_by_sector[s] = top_roa
My result
{'Aerospace & Defense': 177 Lockheed Martin
- Name: company, dtype: object, 'Apparel': 330 Nike
- Name: company, dtype: object, 'Business Services': 433 Adecco Group
Answer code
top_roa_by_sector = {}
for sector in f500["sector"].unique():
is_sector = f500["sector"] == sector
sector_companies = f500.loc[is_sector]
top_company = sector_companies.sort_values("roa",ascending=False).iloc[0]
company_name = top_company["company"]
top_roa_by_sector[sector] = company_name
Answer Result
{'Aerospace & Defense': 'Lockheed Martin',
+ 'Apparel': 'Nike',
+ 'Business Services': 'Adecco Group',
Question: Is the result that I have generated good enough? My assumption is that you are just trying to answer the question which I did, is it necessary to format it “properly” like the answer?