OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

TypeError: Cannot setitem on a categorical with a new category, set the categories first

  • Thread starter Thread starter Namshima
  • Start date Start date
N

Namshima

Guest
Pls am trying to impute missing values in a dataset using Interactive and KNN imputers and before I can do this I have to convert the categorical data in the categorical columns to numerical data after reading the data in using pandas and doing all the necessary stuff i ran the last line of codes I got this error on

TypeError: Cannot setitem on a categorical with a new category, set the categories first

Code:
From sklearn.preprocessing import OrdinalEncoder

encoder = OrdinalEncoder()

Cat_col = df["gender"]

Cat_not_null = cat[cat.notnull()]

reshape_vals = Cat_not_null.values.reshape(-1,1)

Encoded_vals = encoder.fit_transform(reshaped_vals)

df.loc[cat.notnull(), "gender"] = np.squeeze(encoded_vals)

I ran the codes but got the

TypeError: Cannot setitem on a categorical with a new category, set the categories first Tried setting the categories and order them but still got same error
<blockquote>
<p>Pls am trying to impute missing values in a dataset using Interactive and KNN imputers and before I can do this I have to convert the categorical data in the categorical columns to numerical data after reading the data in using pandas and doing all the necessary stuff i ran the last line of codes I got this error on</p>
</blockquote>
<p>TypeError: Cannot setitem on a categorical with a new category, set the categories first</p>
<pre><code>From sklearn.preprocessing import OrdinalEncoder

encoder = OrdinalEncoder()

Cat_col = df["gender"]

Cat_not_null = cat[cat.notnull()]

reshape_vals = Cat_not_null.values.reshape(-1,1)

Encoded_vals = encoder.fit_transform(reshaped_vals)

df.loc[cat.notnull(), "gender"] = np.squeeze(encoded_vals)
</code></pre>
<p>I ran the codes but got the</p>
<p>TypeError: Cannot setitem on a categorical with a new category, set the categories first
Tried setting the categories and order them but still got same error</p>
 

Latest posts

Top