在Pandas中使用groupby来比较一个列中的内容 [英] Use groupby in Pandas to count things in one column in comparison to another
本文介绍了在Pandas中使用groupby来比较一个列中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过它的结果对事件进行分组。这是我的DataFrame(df):
状态事件
成功运行
SUCCESS步行
SUCCESS运行
失败步行
这是我想要的结果:
事件SUCCESS FAILED
运行2 1
步行0 1
我正在尝试制作一个分组对象,但我无法弄清楚如何调用它来显示我想要的。
= pre>
groups = df ['Status']。groupby(df ['Event'])
解决方案
我会做:
df .groupby('Event')。Status.value_counts()。dismack()。fillna(0)
计时
Maybe groupby is the wrong approach. Seems like it should work but I'm not seeing it...
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
Here is my desired result:
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'])
解决方案
I'd do:
df.groupby('Event').Status.value_counts().unstack().fillna(0)
Timing
这篇关于在Pandas中使用groupby来比较一个列中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文