-
Notifications
You must be signed in to change notification settings - Fork 0
/
demographic_data_analyzer.py
58 lines (42 loc) · 3.61 KB
/
demographic_data_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pandas as pd
def calculate_demographic_data():
# Read data from file
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 = pd.Series([df[df['race'] == 'White'].shape[0] , df[df['race'] == 'Black'].shape[0] , df[df['race'] == 'Asian-Pac-Islander'].shape[0] , df[df['race'] == 'Amer-Indian-Eskimo'].shape[0] , df[df['race'] == 'Other'].shape[0]] ,
index=['White', 'Black','Asian-Pac-Islander' ,'Amer-Indian-Eskimo' ,'Other'])
# What is the average age of men?
average_age_men = round (df[df['sex'] == 'Male'] ['age'].mean() , 1)
# What is the percentage of people who have a Bachelor's degree?
percentage_bachelors = round (((df[df['education'] == 'Bachelors'].shape[0]) / (df.shape[0]) ) * 100 ,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 = (df[ (df['education'] == 'Bachelors') | (df['education'] == 'Masters') | (df['education'] == 'Doctorate') ].shape[0])
lower_education = (df.shape[0]) - higher_education
# percentage with salary >50K
higher_education_rich = round (((df[ ((df['education'] == 'Bachelors') | (df['education'] == 'Masters') | (df['education'] == 'Doctorate')) & (df['salary'] == '>50K' ) ].shape[0]) / higher_education ) *100 , 1)
lower_education_rich = round ((((df[ df['salary'] == '>50K'].shape[0]) - (df[ ((df['education'] == 'Bachelors') | (df['education'] == 'Masters') | (df['education'] == 'Doctorate')) & (df['salary'] == '>50K' ) ].shape[0]) ) / lower_education ) *100 , 1)
# What is the minimum number of hours a person works per week (hours-per-week feature)?
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 = df[ df['hours-per-week'] == min_work_hours ].shape[0]
rich_percentage = ((df[ (df['hours-per-week'] == df['hours-per-week'].min()) & (df['salary'] == '>50K' ) ].shape[0] ) / num_min_workers )*100
# What country has the highest percentage of people that earn >50K?
highest_earning_country = ((df[df['salary'] == '>50K']['native-country'].value_counts())/(df['native-country'].value_counts()) ).idxmax()
highest_earning_country_percentage = round (((df[df['salary'] == '>50K']['native-country'].value_counts())/(df['native-country'].value_counts()) *100).max() ,1)
# Identify the most popular occupation for those who earn >50K in India.
top_IN_occupation = df[ (df['salary'] == '>50K') & (df['native-country'] == 'India' ) ]['occupation'].value_counts().idxmax()
# Print out the result
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 0
calculate_demographic_data()