📖 Scenario: Imagine you work in a company that collects customer data. One of the columns in your data is favorite_fruit, which contains names of fruits customers like. You want to prepare this data for a machine learning model, which needs numbers instead of words.
🎯 Goal: You will create a simple one-hot encoding for the favorite_fruit column. This means you will turn each fruit name into a new column with 1 or 0, showing if the customer likes that fruit or not.
📋 What You'll Learn
Create a list called favorite_fruits with exact values: 'apple', 'banana', 'apple', 'orange', 'banana'
Create a list called unique_fruits that contains the unique fruits from favorite_fruits
Create a list of dictionaries called one_hot_encoded where each dictionary represents one customer's one-hot encoding
Print the one_hot_encoded list to show the final result
💡 Why This Matters
🌍 Real World
One-hot encoding is used to convert text categories into numbers so computers can understand and learn from data.
💼 Career
Data scientists and analysts use one-hot encoding to prepare data for machine learning models.
Progress0 / 4 steps
1
Create the favorite fruits list
Create a list called favorite_fruits with these exact values in order: 'apple', 'banana', 'apple', 'orange', 'banana'.
Data Analysis Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Find unique fruits
Create a list called unique_fruits that contains the unique fruits from the favorite_fruits list. Use the set() function and convert it back to a list.
Data Analysis Python
Hint
Use set(favorite_fruits) to get unique items, then list() to convert back to a list.
3
Create one-hot encoding
Create a list called one_hot_encoded. Use a for loop with variable fruit to go through favorite_fruits. For each fruit, create a dictionary where keys are fruits from unique_fruits and values are 1 if the key matches fruit, else 0. Append each dictionary to one_hot_encoded.
Data Analysis Python
Hint
Use a dictionary comprehension inside the loop to create the encoding for each fruit.
4
Print the one-hot encoded list
Print the one_hot_encoded list to display the final one-hot encoding result.
Data Analysis Python
Hint
Use print(one_hot_encoded) to show the final list.
Practice
(1/5)
1. What does one-hot encoding do in data science?
easy
A. It groups data into clusters automatically.
B. It normalizes numerical data to a 0-1 range.
C. It removes missing values from the dataset.
D. It converts categorical data into multiple binary columns.
Solution
Step 1: Understand categorical data representation
One-hot encoding changes categories into separate columns with 1 or 0 to represent presence or absence.
Step 2: Differentiate from other data processing methods
Normalization scales numbers, missing value removal cleans data, clustering groups data; none create binary columns for categories.
Final Answer:
It converts categorical data into multiple binary columns. -> Option D
Quick Check:
One-hot encoding = binary columns for categories [OK]
Hint: One-hot encoding = categories to binary columns [OK]
Common Mistakes:
Confusing one-hot encoding with normalization
Thinking it removes missing data
Assuming it groups data into clusters
2. Which Python code correctly applies one-hot encoding using pandas?
easy
A. pd.get_dummies(df['color'])
B. pd.one_hot(df['color'])
C. df.encode_onehot('color')
D. pd.to_dummies(df['color'])
Solution
Step 1: Recall pandas function for one-hot encoding
The correct function is pd.get_dummies() to convert categorical columns into dummy variables.
Step 2: Check the syntax correctness
Only pd.get_dummies(df['color']) is valid pandas syntax; others are incorrect or non-existent functions.
Final Answer:
pd.get_dummies(df['color']) -> Option A
Quick Check:
Use pd.get_dummies() for one-hot encoding [OK]
Hint: Use pd.get_dummies() to one-hot encode columns [OK]
A. Error because get_dummies cannot be applied on a Series.
B. Error because drop_first is not a valid parameter for get_dummies.
C. No error; code correctly drops the first category column.
D. Error because df.color should be df['color'].
Solution
Step 1: Check get_dummies parameters
The parameter drop_first=True is valid and drops the first category column to avoid multicollinearity.
Step 2: Verify syntax and input type
Using df.color is valid shorthand for df['color'], and get_dummies works on Series.
Final Answer:
No error; code correctly drops the first category column. -> Option C
Quick Check:
drop_first=True is valid in get_dummies [OK]
Hint: drop_first=True drops one dummy column to avoid redundancy [OK]
Common Mistakes:
Thinking drop_first is invalid parameter
Confusing df.color with incorrect syntax
Believing get_dummies only works on DataFrames
5. You have a DataFrame with a column 'city' containing many categories, including some rare ones. How can you apply one-hot encoding but group rare categories into a single 'Other' category before encoding?
hard
A. Replace rare categories with 'Other' in 'city' column, then apply pd.get_dummies().
B. Apply pd.get_dummies() directly; it automatically groups rare categories.
C. Use pd.get_dummies() with parameter group_rare=True.
D. Remove rare categories before applying pd.get_dummies().
Solution
Step 1: Identify rare categories and replace them
Manually replace rare categories in 'city' column with a common label like 'Other' to group them.
Step 2: Apply one-hot encoding on the modified column
Use pd.get_dummies() on the updated 'city' column to create binary columns including 'Other'.
Final Answer:
Replace rare categories with 'Other' in 'city' column, then apply pd.get_dummies(). -> Option A
Quick Check:
Group rare categories manually before encoding [OK]
Hint: Group rare categories manually before one-hot encoding [OK]
Common Mistakes:
Expecting get_dummies to group rare categories automatically
Using non-existent parameters like group_rare
Removing rare categories instead of grouping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.