Python 在分类数据框中查找唯一类值时出错
我有一个dataframe,它有4个特性Python 在分类数据框中查找唯一类值时出错,python,pandas,Python,Pandas,我有一个dataframe,它有4个特性 df A|B|C|D green|big|1.3|4 现在,我将所有对象特性(A和B)放入一个新的数据帧中: df1=df.select_dtypes(include=['object']).columns df1.dtype Out: type('O') 最后一步是将df1输入到函数中,以确定每个分类特征的唯一值 for feature in df1.columns: uniq = np.unique(df1[feature]) p
df
A|B|C|D
green|big|1.3|4
现在,我将所有对象特性(A和B)放入一个新的数据帧中:
df1=df.select_dtypes(include=['object']).columns
df1.dtype
Out: type('O')
最后一步是将df1输入到函数中,以确定每个分类特征的唯一值
for feature in df1.columns:
uniq = np.unique(df1[feature])
print('{}: {} distinct values - {}'.format(feature,len(uniq),uniq))
错误我得到的是:
AttributeError:“Index”对象没有属性“columns”,当我想要获取此属性时:
预期产出:
A: 2 distinct values - ['green' 'blue']
B: 1 distinct values - ['big]
实际上,您的
行有问题。选择\u dtypes
行,您已经访问了列。您应该删除以下内容:
df1=df.select_dtypes(include=['object']) # no .columns
您应该看看您已经为列设置了df1
。@Willen Van Onsem,如果我想排除一些对象列,我将如何在df1=df中指定它。选择数据类型(include=['object'])?
for feature in df1.columns:
uniq = df1[feature].unique()
print('{}: {} distinct values - {}'.format(feature,len(uniq),uniq))