
Answer-first summary for fast verification
Answer: df = pd.get_dummies(df, columns=['Color'], dummy_na=True)
The correct code snippet to perform one-hot encoding on the 'Color' feature while handling missing values is 'df = pd.get_dummies(df, columns=['Color'], dummy_na=True)'. The 'dummy_na=True' parameter ensures that a separate binary column is created for missing values, allowing the model to differentiate between the original missing data and the encoded categories.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
Given a dataset with a categorical feature 'Color' having values 'Red', 'Blue', 'Green', and missing values, write a code snippet to perform one-hot encoding on this feature using Python and the pandas library. Explain how this process handles missing values.
A
df = pd.get_dummies(df, columns=['Color'], dummy_na=True)
B
df = pd.get_dummies(df, columns=['Color'], dummy_na=False)
C
df = pd.get_dummies(df, columns=['Color'], drop_first=True)
D
df = pd.get_dummies(df, columns=['Color'], drop_first=False)
No comments yet.