TypeError: 'OneHotEncoder' object is not iterable

Viewed 1056

I'm trying to use OneHotEncoding on the categorical variables of the following dataset. enter image description here

First, I'm trying to convert the 'Geography' column. Here's what I've done so far:

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer 

ct = ColumnTransformer(['Geography',OneHotEncoder(categories='auto'),[1]],remainder='passthrough')
df_ = ct.fit_transform(df.values)

However, when I try this, I get the following error: error message

Can someone help me to understand why this error occurs and how to solve this?

1 Answers

There is a syntax error in the input parameter to the ColumnTransformer. It expects a list of tuples.

transformers : list of tuples

List of (name, transformer, columns) tuples specifying the transformer objects to be applied to subsets of the data.

Try fixing it by converting the encoder params to a tuple

ct = ColumnTransformer([('Geography',OneHotEncoder(categories='auto'),[1])],remainder='passthrough')
Related