Pivot Table Error

Screen Link:
https://app.dataquest.io/m/348/guided-project%3A-clean-and-analyze-employee-exit-surveys/10/perform-initial-analysis

My Code:

dis_pct = combined_updated.pivot_table(index='service_cat', values='dissatisfied')

What I expected to happen:
A pivot table

What actually happened:
I got an error saying “No numeric types to aggregate”

---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
<ipython-input-55-1d202407851a> in <module>
----> 1 dis_pct = combined_updated.pivot_table(index='service_cat', values='dissatisfied')
      2 
      3 # Create plot
      4 get_ipython().run_line_magic('matplotlib', 'inline')
      5 dis_pct.plot(kind='bar', rot=30)

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\frame.py in pivot_table(self, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed)
   6068         from pandas.core.reshape.pivot import pivot_table
   6069 
-> 6070         return pivot_table(
   6071             self,
   6072             values=values,

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\reshape\pivot.py in pivot_table(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed)
     99 
    100     grouped = data.groupby(keys, observed=observed)
--> 101     agged = grouped.agg(aggfunc)
    102     if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
    103         agged = agged.dropna(how="all")

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func, *args, **kwargs)
    926         func = _maybe_mangle_lambdas(func)
    927 
--> 928         result, how = self._aggregate(func, *args, **kwargs)
    929         if how is None:
    930             return result

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
    309 
    310         if isinstance(arg, str):
--> 311             return self._try_aggregate_string_function(arg, *args, **kwargs), None
    312 
    313         if isinstance(arg, dict):

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\base.py in _try_aggregate_string_function(self, arg, *args, **kwargs)
    265         if f is not None:
    266             if callable(f):
--> 267                 return f(*args, **kwargs)
    268 
    269             # people may try to aggregate on a non-callable attribute

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\groupby\groupby.py in mean(self, *args, **kwargs)
   1222         """
   1223         nv.validate_groupby_func("mean", args, kwargs, ["numeric_only"])
-> 1224         return self._cython_agg_general(
   1225             "mean", alt=lambda x, axis: Series(x).mean(**kwargs), **kwargs
   1226         )

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\groupby\generic.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
    991         self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
    992     ) -> DataFrame:
--> 993         agg_blocks, agg_items = self._cython_agg_blocks(
    994             how, alt=alt, numeric_only=numeric_only, min_count=min_count
    995         )

c:\users\ahmad\appdata\local\programs\python\python38-32\lib\site-packages\pandas\core\groupby\generic.py in _cython_agg_blocks(self, how, alt, numeric_only, min_count)
   1093 
   1094         if not (agg_blocks or split_frames):
-> 1095             raise DataError("No numeric types to aggregate")
   1096 
   1097         if split_items:

DataError: No numeric types to aggregate

Check the solution here:

This person’s issue was that they had switched the “values” and “index” values. That is not my case

Oh sorry about that, I didn’t double check.

Have you filled the missing values with False?

combined_updated['dissatisfied'] = combined_updated['dissatisfied'].fillna(False)

df.pivot_table() method actually treat Boolean values as integers, so a True value is considered to be 1 and a False value is considered to be 0 .

dis_pct = combined_updated.pivot_table(index='service_cat', values='dissatisfied')

This should work since we’ve filled the missing values with False.

I hope above solves your question.