Demographic data - What country has the highest percentage of people that earn >50K?

Tell us what’s happening:
I am getting the result as United States with a percent of 91.4 - which is the incorrect answer.

Your code so far

What country has the highest percentage of people that earn >50K?

totalcount=df[df['salary']=='>50K'].count().iloc[0]
df1 = df[df['salary']=='>50K'].groupby('native-country',as_index=False).size()
df1.columns=['country','50Kcount']
df1['percentcount']=df1['50Kcount']/totalcount*100

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15.

Challenge: Demographic Data Analyzer

Link to the challenge:

Actually, what you’re checking is the percentage of people within people with more than 50K only… (That’s what I’ve done first)…

but actually we need to calculate the percentage of people earning >50K within the country…
Hope you understood…

Won’t the answer still be the United States ? Since the number of rich earners is the highest in USA?

In the above snippet, rich_countries is value_counts of series containing People earning more than 50K…

countries represent value counts of series of people,

Percent is calculated by rich_countries*100/countries

As for the US, Rich people are 7171, all entries are 29170, Percentage = 24.58(Percentage of 7171 people among 29170),

From the last cell, it’s clear that Iran has higher percentage of Rich People than US(From the dataset)

thank you for the explanation… Let me try to implement that now.

hi,
this is the exact point where I am stuck - how to calculate
I have a df called MT which is like countries.head() and another called MT2 which is like rich_countries.head().
this is the code I am working on:

MT = df[df['salary'] == '>50K'].groupby('native-country')['native-country'].count().reset_index(name="Total")

MT2 = df.groupby(["native-country"]).size().reset_index(name="Tot")

MT['percentage'] = np.where(MT['native-country'] == MT2['native-country'], MT['Total']/MT2['tot'],)

any ideas on how to get it to calculate?
thanks and kind regards

Is your code throwing error?

I think you should provide the link to your project, rather than just a snippet…
You can open a new topic as this is regarding different problem.