Tell us what’s happening:
Hi,
I am at an endpoint. I am not sure what is broken or why. This is the error that I get. Please let me know how to fix this. I feel like I’m close but still far away.
raceback (most recent call last):
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py”, line 143, in na_arithmetic_op
result = expressions.evaluate(op, left, right)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/computation/expressions.py”, line 233, in evaluate
return _evaluate(op, op_str, a, b) # type: ignore
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/computation/expressions.py”, line 68, in _evaluate_standard
return op(a, b)
TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “main.py”, line 6, in
demographic_data_analyzer.calculate_demographic_data()
File “/home/runner/boilerplate-demographic-data-analyzer/demographic_data_analyzer.py”, line 38, in calculate_demographic_data
highest_earning_country_percentage = (highest_earning_country / df.groupby(‘native-country’).max()) * 100
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/init.py”, line 651, in f
new_data = self._combine_frame(other, na_op, fill_value)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/frame.py”, line 5870, in _combine_frame
new_data = ops.dispatch_to_series(self, other, _arith_op)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/init.py”, line 275, in dispatch_to_series
bm = left._mgr.operate_blockwise(right._mgr, array_op)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/internals/managers.py”, line 364, in operate_blockwise
return operate_blockwise(self, other, array_op)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/internals/ops.py”, line 38, in operate_blockwise
res_values = array_op(lvals, rvals)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py”, line 190, in arithmetic_op
res_values = na_arithmetic_op(lvalues, rvalues, op)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py”, line 150, in na_arithmetic_op
result = masked_arith_op(left, right, op)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py”, line 92, in masked_arith_op
result[mask] = op(xrav[mask], yrav[mask])
TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’
Your code so far
import pandas as pd
def calculate_demographic_data(print_data=True):
# 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 = df.groupby('race')['race'].count()
# What is the average age of men?
average_age_men = round(df[df['sex'] == 'Male']['age'].mean())
# 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)
# 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'].isin(['Bachelors', 'Masters', 'Doctorate'])]
lower_education = df[~df['education'].isin(['Bachelors', 'Masters', 'Doctorate'])]
# percentage with salary >50K
higher_education_rich = higher_education[higher_education['salary'] == '>50K'].count() / higher_education.shape[0] * 100
lower_education_rich = lower_education[lower_education['salary'] == '>50K'].count() / lower_education.shape[0] *100
# What is the minimum number of hours a person works per week (hours-per-week feature)?
min_work_hours = round(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'] == 1]['hours-per-week'].count()
rich_percentage = df[(df['hours-per-week'] == 1) & (df['salary'] == '>50K')].shape[0] / num_min_workers
# What country has the highest percentage of people that earn >50K?
highest_earning_country = df[df['salary'] == '>50K'].groupby('native-country').max()
highest_earning_country_percentage = (highest_earning_country / df.groupby('native-country').max()) * 100
# Identify the most popular occupation for those who earn >50K in India.
top_IN_occupation= df[(df['native-country'] == 'India') & (df['salary'] == '>50K')].groupby('occupation')['occupation'].count().max()
# 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
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Demographic Data Analyzer
Link to the challenge: