[英]Pandas group by column and count values
我有一個 dataframe:
date code result
2020-01-01 2069.0 Negative
2020-01-29 2069.0 Negative
2020-02-06 2069.0 Positive
2020-02-06 2070.0 Negative
2020-02-07 2070.0 Positive
按代碼分組,我想找出有多少結果 = '正面',有多少結果 = '正面' 和 '負面'。 我對 pandas 很陌生,所以我對所有可用的功能感到很困惑。
謝謝!
您可以嘗試groupby.agg
:
d = dict(zip(['sum','count'],['Positive','Both']))
(df['result'].eq('Positive').view('i1').groupby(df['code']).
agg(['sum','count']).rename(columns=d))
Positive Both
code
2069.0 1 3
2070.0 1 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.