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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With