[英]Pandas groupby counting values > 0
I have a pandas df
of the following format我有以下格式的 pandas df
MATERIAL DATE HIGH LOW
AAA 2022-01-01 10 0
AAA 2022-01-02 0 0
AAA 2022-01-03 5 2
BBB 2022-01-01 0 0
BBB 2022-01-02 10 5
BBB 2022-01-03 8 4
I am looking to transform it such that I land up with the below result我希望对其进行改造,以便得到以下结果
MATERIAL HIGH_COUNT LOW_COUNT
AAA 2 1
BBB 2 2
Essentially for "HIGH_COUNT"
and "LOW_COUNT"
I want to count the number of occurrences that column was greater than 0, grouped by "MATERIAL"
.基本上对于"HIGH_COUNT"
和"LOW_COUNT"
我想计算该列大于 0 的出现次数,按"MATERIAL"
分组。 I have tried to do df.groupby(['MATERIAL']).agg<xxx>
but I am unsure of the agg
function to use here.我试过df.groupby(['MATERIAL']).agg<xxx>
但我不确定在这里使用agg
function 。
Edit:编辑:
I used我用了
df.groupby(['MATERIAL']).agg({'HIGH':'count', 'LOW':'count})
but this counts even the 0
rows.但这甚至计算了0
行。
You could create a boolean DataFrame and groupby
+ sum
:您可以创建一个 boolean DataFrame 和groupby
+ sum
:
out = df[['HIGH', 'LOW']].gt(0).groupby(df['MATERIAL']).sum().add_suffix('_COUNT').reset_index()
Output: Output:
MATERIAL HIGH_COUNT LOW_COUNT
0 AAA 2 1
1 BBB 2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.