From 5143ac88016b4cc44937aa6b883948abdbe91e10 Mon Sep 17 00:00:00 2001 From: Manish Date: Tue, 6 Jun 2023 10:07:13 +1000 Subject: [PATCH] Solution --- demographic_data_analyzer.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/demographic_data_analyzer.py b/demographic_data_analyzer.py index 90a98ec..13992a4 100644 --- a/demographic_data_analyzer.py +++ b/demographic_data_analyzer.py @@ -3,42 +3,33 @@ import pandas as pd def calculate_demographic_data(print_data=True): # Read data from file - df = None + df = pd.read_csv("adult.data.csv") # How many of each race are represented in this dataset? This should be a Pandas series with race names as the index labels. - race_count = None + race_count = df['race'].value_counts() # What is the average age of men? - average_age_men = None + average_age_men = round(df['age'][df['sex'] == 'Male'].mean(), 1) # What is the percentage of people who have a Bachelor's degree? - percentage_bachelors = None + percentage_bachelors = round(df['education'].value_counts(normalize=True).mul(100).loc['Bachelors'], 1) # What percentage of people with advanced education (`Bachelors`, `Masters`, or `Doctorate`) make more than 50K? - # What percentage of people without advanced education make more than 50K? - - # with and without `Bachelors`, `Masters`, or `Doctorate` - higher_education = None - lower_education = None - - # percentage with salary >50K - higher_education_rich = None - lower_education_rich = None + higher_education_rich = round(df['salary'][(df['education'] == 'Bachelors') | (df['education'] == 'Masters') | (df['education'] == 'Doctorate')].value_counts(normalize=True).mul(100).loc['>50K'], 1) + lower_education_rich = round(df['salary'][(df['education'] != 'Bachelors') & (df['education'] != 'Masters') & (df['education'] != 'Doctorate')].value_counts(normalize=True).mul(100).loc[">50K"], 1) # What is the minimum number of hours a person works per week (hours-per-week feature)? - min_work_hours = None + min_work_hours = df['hours-per-week'].min() # What percentage of the people who work the minimum number of hours per week have a salary of >50K? - num_min_workers = None - - rich_percentage = None + rich_percentage = round(df['salary'][df['hours-per-week'] == df['hours-per-week'].min()].value_counts(normalize=True).mul(100).loc[">50K"], 1) # What country has the highest percentage of people that earn >50K? - highest_earning_country = None - highest_earning_country_percentage = None + highest_earning_country = (df['native-country'][df['salary'] == '>50K'].value_counts()/df['native-country'].value_counts()).idxmax() + highest_earning_country_percentage = round((df['native-country'][df['salary'] == '>50K'].value_counts()/df['native-country'].value_counts()).max() * 100, 1) # Identify the most popular occupation for those who earn >50K in India. - top_IN_occupation = None + top_IN_occupation = df['occupation'][(df['native-country'] == 'India') & (df['salary'] == '>50K')].value_counts().idxmax() # DO NOT MODIFY BELOW THIS LINE