[英]Python pandas dataframe: Count number of elements a in column greater or smaller than a threshold
[英]Count number of elements in a column greater than x but smaller than y
我試圖計算'xyz'
列中小於x
但大於y
的元素數。
a= df['xyz']
df[1/3 < a < 2/3].count()
但是,這給了我:
ValueError:系列的真值不明確。 使用a.empty,a.bool(),a.item(),a.any()或a.all()。
不需要設置數據框。 只需對布爾系列求和:
res = df['xyz'].between(1/3, 2/3, inclusive=False).sum()
諸如x < a < y
類的鏈式比較可用於常規Python標量,而不適用於需要向量化操作的Pandas對象。
這將為您提供真正的df:
mask = (df['xyz'] > min) & (df['xyz'] <= max)
df = df[mask]
然后:
len(df) or df.count() or df.shape[0]
要不就:
sum(mask)
a= df['xyz']
df[(a < 2/3) & (a > 1/3)].count()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.