本文介绍了'numpy.ndarray'对象没有属性'count'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下DataFrame:
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:
哪里出错了?
推荐答案
似乎您没有Values
而是values
列,因此需要添加[]
,因为与 values
函数.
It seems you have no Values
but values
column, so need add []
because collision with values
function.
示例:
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
您的解决方案对我来说很好用,我也将.Values
更改为['Values']
.
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
具有 和 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
还要检查>大小和算大熊猫吗?
这篇关于'numpy.ndarray'对象没有属性'count'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!