Demoggraphic analysis highest_earning_country

Hi all,

I’m struggling with the demographic analysis.
I cannot figure out how to get the answer without forming a dataframe.
I get Iran and the percentage correct but its set in table. Please help!. TIA!!

highest_earning_country = (df[df[‘salary’] == ‘>50K’].groupby(‘native-country’).count()/df.groupby(‘native-country’).count()).idxmax()

highest_earning_country_percentage = (df[df[‘salary’] == ‘>50K’].groupby(‘native-country’).count()/df.groupby(‘native-country’).count().round(1)).max() * 100

Can you show your result? It looks like you should get the result in the correct way.
Did you try to split the sum into 2 parts? this might help you finding out where it goes wrong. Something like

percentage = (your calculations)

highest_earning_country = percentage.idxmax()
highest_earning_country_percentage = percentage.max()

I have to use jupiter notebook. for some reason won’t import pandas, but this is the output my code produces:

age Iran
workclass Iran
fnlwgt Iran
education Iran
education-num Iran
marital-status Iran
occupation Iran
relationship Iran
race Iran
sex Iran
capital-gain Iran
capital-loss Iran
hours-per-week Iran
salary Iran
dtype: object
age 41.860465
workclass 41.860465
fnlwgt 41.860465
education 41.860465
education-num 41.860465
marital-status 41.860465
occupation 41.860465
relationship 41.860465
race 41.860465
sex 41.860465
capital-gain 41.860465
capital-loss 41.860465
hours-per-week 41.860465
salary 41.860465
dtype: float64


Well…you are right: those are the correct answers (Iran and 41,9%), but repeated a lot. It cannot be right that the percentage is the same for all those categories, which is suggested by the current results, so there must be something off in your code.

Can you find a way to have the answer showing up only once? Most likely by adjusting the .count a little

Yea that’s the part I’m struggling with. It’s printing the whole dataframe…may take some time to figure out

Is that the output too when you split the calculation into two parts, like I suggested?

The last part about adjusting the .count was a hint, but probably not a very clear one :flushed:
You can add there to make sure only 1 result shows up

(Trying to help you makes me realise that ‘knowing how to do it’ !=
knowing how to explain it’…)

Lol sorry i missed your hint. I’ll be sure have another go at it again soon tweaking the count function.

Thanks, i figured it out now!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.