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 = df[df["sex"]=="Men"]["age"].mean()
# What is the percentage of people who have a Bachelor's degree?
percentage_bachelors = (df[df["education"]=="Bachelors"]["education-num"].count())/(df["education"].count())
# 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 = (df[df["education"].isin(["Bachelors", "Masters", "Doctorate"])]["education-num"].count())/(df["salary"].count())
lower_education_rich = (df[~(df["education"].isin(["Bachelors", "Masters", "Doctorate"]))]["education-num"].count())/(df["salary"].count())
# 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]["hours-per-week"].count()
rich_percentage = (df[(df["hours-per-week"]==min_work_hours)& (df["salary"]==">50K")])/(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()*100).idxmax()
highest_earning_country_percentage =(df[df["salary"]==">50K"]["native-country"].value_counts()/df["native-country"].value_counts()*100).max()
# Identify the most popular occupation for those who earn >50K in India.
top_IN_occupation = (df[(df["native-country"]=="India")&(df["salary"]==">50K")]["occupation"].value_counts().idxmax())
it is not working on my boiler, âhttps://replit.com/@Dandave11/boilerplate-demographic-data-analyzer-3#demographic_data_analyzer.pyâ
response after running the code:
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py", line 166, in _na_arithmetic_op
result = func(left, right)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/computation/expressions.py", line 239, in evaluate
return _evaluate(op, op_str, a, b) # type: ignore[misc]
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/computation/expressions.py", line 69, in _evaluate_standard
return op(a, b)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 6, in <module>
demographic_data_analyzer.calculate_demographic_data()
File "/home/runner/boilerplate-demographic-data-analyzer-3/demographic_data_analyzer.py", line 34, in calculate_demographic_data
rich_percentage = (df[(df["hours-per-week"]==min_work_hours)& (df["salary"]==">50K")])/(num_min_workers)*100
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/common.py", line 69, in new_method
return method(self, other)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/arraylike.py", line 116, in __truediv__
return self._arith_method(other, operator.truediv)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/frame.py", line 6866, in _arith_method
new_data = self._dispatch_frame_op(other, op, axis=axis)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/frame.py", line 6893, in _dispatch_frame_op
bm = self._mgr.apply(array_op, right=right)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 325, in apply
applied = b.apply(f, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 381, in apply
result = func(self.values, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py", line 224, in arithmetic_op
res_values = _na_arithmetic_op(left, right, op)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py", line 173, 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 131, in _masked_arith_op
result[mask] = op(xrav[mask], y)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
exit status 1
îș§