在 Pandas 中使用 groupby 将一列中的内容与另一列进行比较 [英] Use groupby in Pandas to count things in one column in comparison to another

查看:46
本文介绍了在 Pandas 中使用 groupby 将一列中的内容与另一列进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许 groupby 是错误的方法.似乎它应该工作,但我没有看到它......

Maybe groupby is the wrong approach. Seems like it should work but I'm not seeing it...

我想根据结果对事件进行分组.这是我的 DataFrame (df):

I want to group an event by it's outcome. Here is my DataFrame (df):

Status  Event
SUCCESS Run
SUCCESS Walk
SUCCESS Run
FAILED  Walk

这是我想要的结果:

Event   SUCCESS FAILED
Run     2       1
Walk    0       1

我正在尝试创建一个分组对象,但我不知道如何调用它来显示我想要的内容.

I'm trying to make a grouped object but I can't figure out how to call it to display what I want.

grouped = df['Status'].groupby(df['Event'])

推荐答案

我会:

df.groupby('Event').Status.value_counts().unstack().fillna(0)

或者使用 fill_value 参数:

df.groupby('Event').Status.value_counts().unstack(fill_value=0)

这篇关于在 Pandas 中使用 groupby 将一列中的内容与另一列进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆