[英]How to groupby count across multiple columns in pandas
I have the following sample dataframe in Python pandas: 我在Python大熊猫中有以下示例数据框:
+---+------+------+------+
| | col1 | col2 | col3 |
+---+------+------+------+
| 0 | a | d | b |
+---+------+------+------+
| 1 | a | c | b |
+---+------+------+------+
| 2 | c | b | c |
+---+------+------+------+
| 3 | b | b | c |
+---+------+------+------+
| 4 | a | a | 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: 我想对第1-3列中的所有'a','b','c'和'd'值进行计数,以便得到这样的数据框:
+---+--------+-------+
| | 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? 有人可以帮我弄这个吗?
You can stack()
the dataframe to put all columns into rows and then do value_counts
: 您可以stack()
数据框将所有列放入行,然后执行value_counts
:
df.stack().value_counts()
b 5
c 4
a 4
d 2
dtype: int64
You can apply
value_counts
with sum
: 您可以apply
value_counts
与sum
:
print (df.apply(pd.value_counts))
col1 col2 col3
a 3.0 1 NaN
b 1.0 2 2.0
c 1.0 1 2.0
d NaN 1 1.0
df1 = df.apply(pd.value_counts).sum(1).reset_index()
df1.columns = ['letter','count']
df1['count'] = df1['count'].astype(int)
print (df1)
letter count
0 a 4
1 b 5
2 c 4
3 d 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.