pandas 类别子组0个计数 [英] Pandas Category sub-group 0 counts

查看:43
本文介绍了 pandas 类别子组0个计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Pandas的新手,它试图生成一个维持类别顺序并显示零计数的子组计数表.这是一个简单的类别,有4个选项.

I'm new to Pandas, and trying to generate a table of subgroup counts maintaining the category order, and showing zero counts. It's a simple category with 4 options.

不进行分组,它可以按预期方式工作,但是进行分组时,它不会显示零计数(请参阅最后一组).

Without grouping it works as expected, but with grouping it isn't displaying zero counts (see last group).

在[21]中:

df2['Call_cat'] = df2['Group_INV'].astype('category')
counts = df2['Call_cat'].value_counts(dropna=True, sort=False)
counts

出[21]:

1.0    35773
2.0      290
3.0     8680
4.0    18867
Name: Call_cat, dtype: int64

在[22]中:

grouped = df2.groupby('Z1')['Call_cat'].value_counts(dropna = True,sort = False) 分组

grouped = df2.groupby('Z1')['Call_cat'].value_counts(dropna=True, sort=False) grouped

出[22]:

Z1    Call_cat
ZLZO  1.0         2961
      2.0           24
      3.0          476
      4.0         1221
ZZRE  1.0          307
      2.0            2
      3.0          152
      4.0          197
ZZMB  1.0          904
      3.0          198
      4.0          906

推荐答案

您可以从两个分组列的值的所有组合中创建一个MultiIndex,并使用此多索引对groupby结果进行重新索引.然后用零填充NaN值.

You can create a MultiIndex from all combinations of values of two grouping columns and reindex the groupby result with this multiindex. Then fill NaN values with zeros.

import pandas as pd
# example data
df = pd.DataFrame({'a':list('xxxyyy'), 'b':[1,2,3,1,2,2]})
#    a  b
# 0  x  1
# 1  x  2
# 2  x  3
# 3  y  1
# 4  y  2
# 5  y  2

multi_index = pd.MultiIndex.from_product([df.a.unique(), df.b.unique()], 
                                         names=['a', 'b'])\
                           .sort_values()
df.groupby(['a','b']).size().reindex(multi_index).fillna(0).astype(int)

这产生

a  b
x  1    1
   2    1
   3    1
y  1    1
   2    2
   3    0

这篇关于 pandas 类别子组0个计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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