Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在分类数据框中查找唯一类值时出错_Python_Pandas - Fatal编程技术网

Python 在分类数据框中查找唯一类值时出错

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

我有一个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])
    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))