![](/img/trans.png)
[英]counting the number of non-zero numbers in a column of a df in pandas/python
[英]Counting the number of zero's in a column
df = pd.DataFrame({'Score':[1,2,3,4,5], 'Rate': [1.0, 0.0, 0.0, 0.5, 0.5]})
df.head()
Score Rate
0 1 1.0
1 2 0.0
2 3 0.0
3 4 0.5
4 5 0.5
我正在嘗試計算 Rates 等於 0 的數量並顯示相應的分數
你可以使用 loc:
df.loc[df.Rate.eq(0)]
Score Rate
1 2 0.0
2 3 0.0
或者,如果您只需要查看分數:
df.loc[df.Rate.eq(0)].Score
將DataFrame.loc
與boolean indexing
一起使用,然后獲取列Score
的計數和總和:
df = df.loc[df['Rate'].eq(0), 'Score'].agg(['sum','size'])
print (df)
sum 5
size 2
Name: Score, dtype: int64
print(df[df['Rate']==0]['Score'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.