Intermediate step 12: top ROA by sector

Screen Link:

My Code:

f500['roa']=f500['profits']/f500['assets']
top_roa_by_sector={}
    
sectors=f500['sector'].unique()

for s in sectors:
    sorted_roa=f500[f500['sector']==s].sort_values('roa',ascending=False).head(1)
    top_roa_by_sector[s]=sorted_roa['company']
    

I expected a dictionary with the sectors as keys and the key values as company names. It should look like this

{'Retailing': 'H & M Hennes & Mauritz',
+  'Energy': 'National Grid',
+  'Motor Vehicles & Parts': 'Subaru',
+  'Financials': 'Berkshire Hathaway',
+  'Technology': 'Accenture',
+  'Wholesalers': 'McKesson',
+  'Health Care': 'Gilead Sciences',
+  'Telecommunications': 'KDDI',
+  'Engineering & Construction': 'Pacific Construction Group',
+  'Industrials': '3M',
+  'Food & Drug Stores': 'Publix Super Markets',
+  'Aerospace & Defense': 'Lockheed Martin',
+  'Food, Beverages & Tobacco': 'Philip Morris International',
+  'Household Products': 'Unilever',
+  'Transportation': 'Delta Air Lines',
+  'Materials': 'CRH',
+  'Chemicals': 'LyondellBasell Industries',
+  'Media': 'Disney',
+  'Apparel': 'Nike',
+  'Hotels, Restaurants & Leisure': 'McDonald’s',
+  'Business Services': 'Adecco Group'}

What actually happened: Instead my code displays the company rank alongside the company. I also get data types

{'Retailing': 481    H & M Hennes & Mauritz
-  Name: company, dtype: object,
-  'Energy': 490    National Grid
-  Name: company, dtype: object,
-  'Motor Vehicles & Parts': 351    Subaru
-  Name: company, dtype: object,
-  'Financials': 7    Berkshire Hathaway
-  Name: company, dtype: object,
-  'Technology': 304    Accenture
-  Name: company, dtype: object,
-  'Wholesalers': 10    McKesson
-  Name: company, dtype: object,
-  'Health Care': 357    Gilead Sciences
-  Name: company, dtype: object,
-  'Telecommunications': 218    KDDI
-  Name: company, dtype: object,
-  'Engineering & Construction': 88    Pacific Construction Group
-  Name: company, dtype: object,
-  'Industrials': 360    3M
-  Name: company, dtype: object,
-  'Food & Drug Stores': 307    Publix Super Markets
-  Name: company, dtype: object,
-  'Aerospace & Defense': 177    Lockheed Martin
-  Name: company, dtype: object,
-  'Food, Beverages & Tobacco': 405    Philip Morris International
-  Name: company, dtype: object,
-  'Household Products': 149    Unilever
-  Name: company, dtype: object,
-  'Transportation': 256    Delta Air Lines
-  Name: company, dtype: object,
-  'Materials': 362    CRH
-  Name: company, dtype: object,
-  'Chemicals': 373    LyondellBasell Industries
-  Name: company, dtype: object,
-  'Media': 160    Disney
-  Name: company, dtype: object,
-  'Apparel': 330    Nike
-  Name: company, dtype: object,
-  'Hotels, Restaurants & Leisure': 435    McDonald’s
-  Name: company, dtype: object,
-  'Business Services': 433    Adecco Group
-  Name: company, dtype: object}

Why does my code display unintended extra setails? Thankyou for your help

Your sorted_roa is a DataFrame. When you try to access a column from a DataFrame you get a Series.

That’s what sorted_roa['company'] returns, a Series. And that’s what you store in your dictionary.

What you need to do is to extract only the values in that column/Series at each iteration. There are different ways to do this, but as a start try to see how you could use something like loc or iloc for that purpose.

1 Like