Assignment 1: Sorting prior to encoding

Prior to performing the encoding in Question 7, we sort the categorical columns. The pre-written code is as seen below.

inputs_df[categorical_cols].nunique().sort_values(ascending=False)

Unfortunately, it gives me the value error message that I will attach below. I am having a difficult time figuring out what is wrong with it. Any help would be appreciated.

ValueError Traceback (most recent call last)
/tmp/ipykernel_35/2340213611.py in
----> 1 inputs_df[categorical_cols].nunique().sort_values(ascending=False)

/opt/conda/lib/python3.9/site-packages/pandas/core/frame.py in getitem(self, key)
3443 # Do we have a (boolean) DataFrame?
3444 if isinstance(key, DataFrame):
→ 3445 return self.where(key)
3446
3447 # Do we have a (boolean) 1d indexer?

/opt/conda/lib/python3.9/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
309 stacklevel=stacklevel,
310 )
→ 311 return func(*args, **kwargs)
312
313 return wrapper

/opt/conda/lib/python3.9/site-packages/pandas/core/frame.py in where(self, cond, other, inplace, axis, level, errors, try_cast)
10735 try_cast=lib.no_default,
10736 ):

10737 return super().where(cond, other, inplace, axis, level, errors, try_cast)
10738
10739 @deprecate_nonkeyword_arguments(

/opt/conda/lib/python3.9/site-packages/pandas/core/generic.py in where(self, cond, other, inplace, axis, level, errors, try_cast)
9030 )
9031
→ 9032 return self._where(cond, other, inplace, axis, level, errors=errors)
9033
9034 @final

/opt/conda/lib/python3.9/site-packages/pandas/core/generic.py in _where(self, cond, other, inplace, axis, level, errors)
8786 for dt in cond.dtypes:
8787 if not is_bool_dtype(dt):
→ 8788 raise ValueError(msg.format(dtype=dt))
8789 else:
8790 # GH#21947 we have an empty DataFrame/Series, could be object-dtype

ValueError: Boolean array expected for the condition, not object

It’s hard to deduce what’s going on without code where you encode the stuff and where you acquire variables you use there.

I think that I figured it out. my definition of categorical_cols only included object dtype not boolean. I added that, and now it works.

Thanks for your quick support.