Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a dataframe column which has been discretized using pd.cut. While trying to work with an algorithm, requires me to do the following:
Python
for k, v in dictMap.items():
 df.loc[k, column] = v 


This gives me an error:
<pre lang="Python">>ValueError: Cannot setitem on a Categorical with a new category, set the categories first
.

After discretizing, Column A contains values [1,2,3,4,5].

What I have tried:

After figuring out the datatype I have tried to convert the column dtype using:
Python
df["A"] = df ["A"].apply(lambda x: x.cat.codes)
.

This now gives me the error:
AttributeError: long object has no attribute cat
.

print(df["A"])

outputs:
Name:A, Length 891, dtype:category
Categories(25, int64): [0<1<2<3...21<22<23<24]


My goal is to have this discretized column in the dataframe converted to the dtype int.
Posted
Updated 28-Jun-17 1:50am
v3

1 solution

Got it working as an object with:
Python
df["A"]=df.A.astype(object)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900