pandas 'numpy.ndarray' 对象没有属性 'count'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42751662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:10:03  来源:igfitidea点击:

'numpy.ndarray' object has no attribute 'count'

pythonpandasnumpy

提问by Niko

I have the following DataFrame:

我有以下数据帧:

df  = pd.DataFrame({'Label': list('AABCCC'), 'Values':  [1,2,3,4,np.nan,8] })

I want to drop those groups that do not have a minimum number of items (one or less) so I tried the following:

我想删除那些没有最少项目数(一个或更少)的组,所以我尝试了以下操作:

f = lambda x: x.Values.count() > 1

df.groupby('Label').filter(f)

However, this raised an error:

然而,这引发了一个错误:

Error : 'numpy.ndarray' object has no attribute 'count'

错误:'numpy.ndarray' 对象没有属性 'count'

Where did it go wrong?

哪里出错了?

采纳答案by jezrael

It seems you have no Valuesbut valuescolumn, so need add []because collision with valuesfunction.

看来你没有Values,但values列,因此需要添加[],因为与碰撞values的功能。

Sample:

样本:

df = pd.DataFrame ({'values': [1,2,3,4,np.nan,8] })
print (df)
   values
0     1.0
1     2.0
2     3.0
3     4.0
4     NaN
5     8.0

#return numpy array
print (df.values)
[[  1.]
 [  2.]
 [  3.]
 [  4.]
 [ nan]
 [  8.]]

#select column values
print (df['values'])
0    1.0
1    2.0
2    3.0
3    4.0
4    NaN
5    8.0
Name: values, dtype: float64

Your solution for me works nice, I also change .Valuesto ['Values'].

您对我的解决方案效果很好,我也更改.Values['Values'].

df1 = df.groupby('Label').filter(lambda x: x['Values'].count() > 1)
print (df1)
  Label  Values
0     A     1.0
1     A     2.0
3     C     4.0
4     C     NaN
5     C     8.0

Alternative solution with transformand boolean indexing:

使用transform和 的替代解决方案boolean indexing

print (df.groupby('Label')['Values'].transform('count'))
0    2.0
1    2.0
2    1.0
3    2.0
4    2.0
5    2.0
Name: Values, dtype: float64

print (df.groupby('Label')['Values'].transform('count') > 1)
0     True
1     True
2    False
3     True
4     True
5     True
Name: Values, dtype: bool

print (df[df.groupby('Label')['Values'].transform('count') > 1])
  Label  Values
0     A     1.0
1     A     2.0
3     C     4.0
4     C     NaN
5     C     8.0

Also check What is the difference between size and count in pandas?

还要检查Pandas的大小和数量有什么区别?