close

[Solved] ‘numpy.ndarray’ object has no attribute ‘count’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ‘numpy.ndarray’ object has no attribute ‘count’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ‘numpy.ndarray’ object has no attribute ‘count’ Error Occurs?

Today I get the following error ‘numpy.ndarray’ object has no attribute ‘count’ in python.

How To Solve ‘numpy.ndarray’ object has no attribute ‘count’ Error ?

  1. How To Solve 'numpy.ndarray' object has no attribute 'count' Error ?

    To Solve 'numpy.ndarray' object has no attribute 'count' Error It seems you have no Values but values column, so need add [] because collision with values function.

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

    To Solve 'numpy.ndarray' object has no attribute 'count' Error It seems you have no Values but values column, so need add [] because collision with values function.

Solution 1

It seems you have no Values but values column, so need add [] because collision with values function.

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 .Values to ['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 transform and 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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read