292-12 Exploring Data with Pandas : Intermediate Screen 12

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?

Hey, Gen.

It’s good enough in the sense that it finds an answer to the business question. But, in order for us to properly check your answer, we try to give precise instructions. Regarding the instruction

Dictionary values with the company name with the highest ROA value from that sector.

You’re passing the name as a series object, but names typically are saved as strings, not as series.

You only need to add .iloc[0] in an appropriate place for your code to pass.

1 Like