Python:attributeerror:long对象没有属性cat [英] Python : attributeerror: long object has no attribute cat
本文介绍了Python:attributeerror:long对象没有属性cat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用pd.cut离散化的数据帧列。在尝试使用算法时,需要我执行以下操作:
k,v < span class =code-keyword> in dictMap.items():
df.loc [k,column] = v
这给了我一个错误:
< pre lang =Python>> ValueError:无法在具有新类别的分类上setitem ,首先设置类别
。
离散后,A列包含值[1,2,3,4,5]。 />
我尝试了什么:
找出我尝试过的数据类型使用以下方法转换列dtype:
df [ ] = df [ A]。apply( lambda x:x.cat.codes)
。
现在这给了我错误:
AttributeError: long对象没有属性cat
。
print(df [A])
输出:
名称:A,长度891,dtype:category
类别(25,int64) :[0 <1 <3< 3< 3< 3< 3< 3< 3< 3< 2< 3< 3< 3< 3< 3>
数据帧中的列转换为dtype int。解决方案
让它作为一个对象工作:
df [ A] = df.A.astype(object)
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:
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:
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.解决方案Got it working as an object with:
df["A"]=df.A.astype(object)
这篇关于Python:attributeerror:long对象没有属性cat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文