如何对 pandas 中的多个列进行分组依据 [英] How to groupby count across multiple columns in pandas

查看:70
本文介绍了如何对 pandas 中的多个列进行分组依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python大熊猫中有以下示例数据框:

I have the following sample dataframe in Python pandas:

+---+------+------+------+
|   | col1 | col2 | col3 |
+---+------+------+------+
| 0 |   a  |   d  |   b  |
+---+------+------+------+
| 1 |   a  |   c  |   b  |
+---+------+------+------+
| 2 |   c  |   b  |   c  |
+---+------+------+------+
| 3 |   b  |   b  |   c  |
+---+------+------+------+
| 4 |   a  |   a  |   d  |
+---+------+------+------+

我想对第1-3列中的所有'a','b','c'和'd'值进行计数,以便最终得到一个数据框像这样:

I would like to perform a count of all the 'a,' 'b,' 'c,' and 'd' values across columns 1-3 so that I would end up with a dataframe like this:

+---+--------+-------+
|   | letter | count |
+---+--------+-------+
| 0 |    a   |   4   |
+---+--------+-------+
| 1 |    b   |   5   |
+---+--------+-------+
| 2 |    c   |   4   |
+---+--------+-------+
| 3 |    d   |   2   |
+---+--------+-------+

我可以这样做的一种方法是将各列彼此堆叠,然后进行分组计数,但是我觉得必须有更好的方法。有人可以帮我吗?

One way I can do this is stack the columns on top of each other and THEN do a groupby count, but I feel like there has to be a better way. Can someone help me with this?

推荐答案

您可以 stack()数据框将所有列放入行,然后执行 value_counts

You can stack() the dataframe to put all columns into rows and then do value_counts:

df.stack().value_counts()

b    5
c    4
a    4
d    2
dtype: int64

这篇关于如何对 pandas 中的多个列进行分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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