1. Learn
  2. /
  3. Courses
  4. /
  5. Dealing with Missing Data in Python

Exercise

Ordinal encoding of a DataFrame

Categorical features can be encoded using two techniques namely, one-hot encoding and ordinal encoding. In one-hot encoding, each category becomes a column and the respective category column for each row is 1 and the others 0. In ordinal encoding, the categories are mapped to integer values starting from 0 to number of categories.

In this exercise, you will loop over all the columns in the users DataFrame to ordinally encode the categories. You will also store an encoder for each column in a dictionary ordinal_enc_dict so that the encoded columns can be converted back to the original categories.

Instructions

100 XP
  • Define an empty dictionary ordinal_enc_dict.
  • Create an Ordinal Encoder object for each column.
  • Select non-null values of column in users and encode them.
  • Assign back the encoded values to non-null values of each column (col_name) in users.