Exploring data with Pandas intermediate - 12/14

Screen Link:

Hi,
I have been working my way through the mission and hit a bit of a wall in step 12 of the exploring data with pandas - intermediate mission. I followed the step 11 very closely to reach my answer, perhaps too much as I am not getting the correct answer.
My Code:

f500["roa"] = f500["profits"]/f500["assets"]

top_roa_by_sector = {}

sectors = f500["sector"].unique()

for s in sectors:
    selected_rows = f500[f500["sector"] == s]
    sorted_rows = selected_rows.sort_values("roa",ascending=False)
    top_company = selected_rows.iloc[0]
    company_name = top_company["company"]
    top_roa_by_sector[s] = company_name

What I expected to happen:
I expected to receive the answer below:

{'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:
This is the answer I obtained

{'Retailing': 'Walmart',
 'Energy': 'State Grid',
 'Motor Vehicles & Parts': 'Toyota Motor',
 'Financials': 'Berkshire Hathaway',
 'Technology': 'Apple',
 'Wholesalers': 'McKesson',
 'Health Care': 'UnitedHealth Group',
 'Telecommunications': 'AT&T',
 'Engineering & Construction': 'China State Construction Engineering',
 'Industrials': 'General Electric',
 'Food & Drug Stores': 'Walgreens Boots Alliance',
 'Aerospace & Defense': 'Boeing',
 'Food, Beverages & Tobacco': 'Nestle',
 'Household Products': 'Procter & Gamble',
 'Transportation': 'U.S. Postal Service',
 'Materials': 'China Minmetals',
 'Chemicals': 'BASF',
 'Media': 'Disney',
 'Apparel': 'Christian Dior',
 'Hotels, Restaurants & Leisure': 'Compass Group',
 'Business Services': 'Adecco Group'}

Thanks for any help. Hope I have made this clear enough! Sorry my first question on here!

@bottom_line20 I think you will need to describe or cut and paste the expected answer here for the best assistance. Or link to the mission page for easy reference.

Hi @bottom_line20 and welcome to the community!

As @kwu mentioned, it would really help to have a link to the mission in order to test out your code with the dataset…because without the data, we can’t really test it! That said, I did manage to manually wade through the missions and find the one you’re on.

I have to admit, this one stumped me for longer than I would have liked! To troubleshoot it, I inserted print statements inside the for loop in order to determine where things were going off the rails because at first glance, your code looks good! Here is an example of how I tested the variable selected_rows:

for s in sectors:
    selected_rows = f500[f500["sector"] == s]
    print(selected_rows.head())
    sorted_rows = selected_rows.sort_values("roa",ascending=False)
    top_company = selected_rows.iloc[0]
    company_name = top_company["company"]
    top_roa_by_sector[s] = company_name

The results of that looked fine so I “moved down a line” and tested out sorted_rows:

for s in sectors:
    selected_rows = f500[f500["sector"] == s]
    sorted_rows = selected_rows.sort_values("roa",ascending=False)
    print(sorted_rows.head())
    top_company = selected_rows.iloc[0]
    company_name = top_company["company"]
    top_roa_by_sector[s] = company_name

and that too looked fine! But when I moved to inspect top_company, I found there was a problem. It was no longer what I expected. Therefore, I can be sure your problem comes from this line of code:

top_company = selected_rows.iloc[0]

Can you see what’s causing the problem? If you can’t, no worries, let me know and we can try something else.

EDIT_1:
You may want to take a look at this post for some helpful tips on how to post a technical question in the community. Short of the long: always include a link to the mission in question and try to provide as much information as possible (like the expected result or error codes you receive).

Hey,
Thanks for both of your replies. Apologies for stuffing up asking the question in a clear manner! I’ll read the guide you posted.

I feel like I am being very slow and still not seeing how to adjust the code to fix the problem and obtain the answer.

After staring at the code for longer than I would like to admit and going back through the previous steps of the mission. I finally realised my mistake in the line of code you highlighted the mistake was in

top_company = selected_rows.iloc[0]

Needed adjusting to

top_company = sorted_rows.iloc[0]

Thank you! Also thanks for posting your logic to how you found which line of code was causing the issue.

Bravo! Nicely done! As I said, it took me a while too and felt I shouldn’t deny you the chance to figure it out (with a LITTLE assistance) because it’s a really valuable skill to troubleshoot code…also it feels really good (and a little bad) when you do eventually find out where things went wrong.

Glad I was able to help you out!

Happy coding.