jbae42
jbae42

Reputation: 27

TypeError: 'OneHotEncoder' object is not iterable

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?

Upvotes: 2

Views: 4331

Answers (1)

jdsurya
jdsurya

Reputation: 1396

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')

Upvotes: 3

Related Questions