DATA VISUALIZATION. EDA - Lab Problem 7
Здравейте,
Имам проблем с проверката на седма задача от лаба.
Problem 7. Busiest occupations (2 points)
Which people are most overworked? Group all data by occupation and calculate the mean hours per week for each group.
Write a function that calculates and returns all mean hours per week as a
Series
. Sort the results in descending order (most hours to fewest hours).
Това решението:
def get_mean_working_hours_by_income(dataframe):
return dataframe.groupby("occupation")["hours_per_week"].mean().sort_values(ascending = False)
А това излиза при изпълнение:
occupation
Farming-fishing 46.989940
Exec-managerial 44.987703
Transport-moving 44.656230
Protective-serv 42.870570
Prof-specialty 42.386715
Craft-repair 42.304221
Sales 40.781096
Machine-op-inspct 40.755744
Armed-Forces 40.666667
Tech-support 39.432112
Handlers-cleaners 37.947445
Adm-clerical 37.558355
Other-service 34.701669
Priv-house-serv 32.885906
? 31.906131
Name: hours_per_week, dtype: float64
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4380 try:
-> 4381 return libindex.get_value_box(s, key)
4382 except IndexError:
pandas/_libs/index.pyx in pandas._libs.index.get_value_box()
pandas/_libs/index.pyx in pandas._libs.index.get_value_at()
pandas/_libs/util.pxd in pandas._libs.util.get_value_at()
pandas/_libs/util.pxd in pandas._libs.util.validate_indexer()
TypeError: 'str' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-31-96210d0cf261> in <module>
1 hours = get_mean_working_hours_by_income(income_data)
2 print(hours)
----> 3 nose.tools.assert_almost_equal(hours["Handlers-cleaners"], 37.95, delta = 0.01)
~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
866 key = com.apply_if_callable(key, self)
867 try:
--> 868 result = self.index.get_value(self, key)
869
870 if not is_scalar(result):
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4387 raise InvalidIndexError(key)
4388 else:
-> 4389 raise e1
4390 except Exception: # pragma: no cover
4391 raise e1
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4373 try:
4374 return self._engine.get_value(s, k,
-> 4375 tz=getattr(series.dtype, 'tz', None))
4376 except KeyError as e1:
4377 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Handlers-cleaners'
Като цяло не мога да разбера къде може да съм сбъркал. Функцията връща:
pandas.core.series.Series
, което изпълнява заданието.
Хистограмата ми излиза правилно.
Някой има ли подобен проблем? Моля за помощ...