2020-09-29 19:23:25 +02:00
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_demographic_data(print_data=True):
|
|
|
|
# Read data from file
|
2023-06-06 02:07:13 +02:00
|
|
|
df = pd.read_csv("adult.data.csv")
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# How many of each race are represented in this dataset? This should be a Pandas series with race names as the index labels.
|
2023-06-06 02:07:13 +02:00
|
|
|
race_count = df['race'].value_counts()
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# What is the average age of men?
|
2023-06-06 02:07:13 +02:00
|
|
|
average_age_men = round(df['age'][df['sex'] == 'Male'].mean(), 1)
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# What is the percentage of people who have a Bachelor's degree?
|
2023-06-06 02:07:13 +02:00
|
|
|
percentage_bachelors = round(df['education'].value_counts(normalize=True).mul(100).loc['Bachelors'], 1)
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# What percentage of people with advanced education (`Bachelors`, `Masters`, or `Doctorate`) make more than 50K?
|
2023-06-06 02:07:13 +02:00
|
|
|
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)
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# What is the minimum number of hours a person works per week (hours-per-week feature)?
|
2023-06-06 02:07:13 +02:00
|
|
|
min_work_hours = df['hours-per-week'].min()
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# What percentage of the people who work the minimum number of hours per week have a salary of >50K?
|
2023-06-06 02:07:13 +02:00
|
|
|
rich_percentage = round(df['salary'][df['hours-per-week'] == df['hours-per-week'].min()].value_counts(normalize=True).mul(100).loc[">50K"], 1)
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# What country has the highest percentage of people that earn >50K?
|
2023-06-06 02:07:13 +02:00
|
|
|
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)
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# Identify the most popular occupation for those who earn >50K in India.
|
2023-06-06 02:07:13 +02:00
|
|
|
top_IN_occupation = df['occupation'][(df['native-country'] == 'India') & (df['salary'] == '>50K')].value_counts().idxmax()
|
2020-09-29 19:23:25 +02:00
|
|
|
|
|
|
|
# DO NOT MODIFY BELOW THIS LINE
|
|
|
|
|
|
|
|
if print_data:
|
|
|
|
print("Number of each race:\n", race_count)
|
|
|
|
print("Average age of men:", average_age_men)
|
|
|
|
print(f"Percentage with Bachelors degrees: {percentage_bachelors}%")
|
|
|
|
print(f"Percentage with higher education that earn >50K: {higher_education_rich}%")
|
|
|
|
print(f"Percentage without higher education that earn >50K: {lower_education_rich}%")
|
|
|
|
print(f"Min work time: {min_work_hours} hours/week")
|
|
|
|
print(f"Percentage of rich among those who work fewest hours: {rich_percentage}%")
|
|
|
|
print("Country with highest percentage of rich:", highest_earning_country)
|
|
|
|
print(f"Highest percentage of rich people in country: {highest_earning_country_percentage}%")
|
|
|
|
print("Top occupations in India:", top_IN_occupation)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'race_count': race_count,
|
|
|
|
'average_age_men': average_age_men,
|
|
|
|
'percentage_bachelors': percentage_bachelors,
|
|
|
|
'higher_education_rich': higher_education_rich,
|
|
|
|
'lower_education_rich': lower_education_rich,
|
|
|
|
'min_work_hours': min_work_hours,
|
|
|
|
'rich_percentage': rich_percentage,
|
|
|
|
'highest_earning_country': highest_earning_country,
|
|
|
|
'highest_earning_country_percentage':
|
|
|
|
highest_earning_country_percentage,
|
|
|
|
'top_IN_occupation': top_IN_occupation
|
|
|
|
}
|