10

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'

Where did it go wrong?

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Niko
  • 103
  • 1
  • 1
  • 6

1 Answers1

6

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

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

Graham
  • 7,431
  • 18
  • 59
  • 84
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you for accepting. Btw, in my opinion select with `.` is nicer and faster, but then no column `values`, `sum`, `count`, `mean` because `.sum`, `.values`... return pandas functions. So safer is use `[]` and then all column names works perfectly. Nice day and good luck! – jezrael Mar 12 '17 at 19:34