简体   繁体   English

Pandas 按列和计数值分组

[英]Pandas group by column and count values

I have a dataframe:我有一个 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

Grouping by code, I want to find how many results = 'Positive', and how many results = 'Positive' AND 'Negative'.按代码分组,我想找出有多少结果 = '正面',有多少结果 = '正面' 和 '负面'。 I'm quite new to pandas so I'm quite confused with all the functions that are available.我对 pandas 很陌生,所以我对所有可用的功能感到很困惑。

Thanks!谢谢!

You can try groupby.agg :您可以尝试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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM