在pandas中,是否有一种方法可以对另一列进行分组,然后在另一列具有指定值的地方进行唯一计数?

问题描述

我有一个包含许多列的pandas数据框。为简单起见,假设列为“国家”,“时间段”,“类别”和“ id”。 “类别”可以是“职员”或“学生”。

import pandas as pd
    data = {'country':  ['A','A','B',],'time_bucket': ['8','8','9'],'category': ['staff','staff','student','staff'],'id': ['101','172','122','142','132'],}
        
        df = pd.DataFrame (data,columns = ['country','time_bucket','category','id'])
df


country time_bucket category    id
0   A      8      staff        101
1   A      8      staff        172
2   A      8      student      122
3   B      8      student      142
4   B      9      staff        132

我想找出特定时间间隔内一个国家的教职员工总数和学生总数,并将它们添加为新列。

我可以得出一个特定时间段内一个国家的总人数:

df['persons_count'] = df.groupby(['time_bucket','country'])['id'].transform('nunique')

country time_bucket category    id  persons_count
0   A      8         staff      101    3
1   A      8         staff      172    3
2   A      8         student    122    3
3   B      8         student    142    1
4   B      9         staff      132    1

但是,我不知道如何考虑“类型”并将其添加到我的代码中。

我想要这样的东西:

country time_bucket category    id  staff_count student_count
0   A     8          staff      101     2           1  
1   A     8          staff      172     2           1
2   A     8          student    122     2           1
3   B     8          student    142     0           1
4   B     9          staff      132     1           0

任何建议将不胜感激!


添加一个新示例,该示例显示需要唯一的“ id”计数

import pandas as pd
data = {'country':  ['A',}
        
df = pd.DataFrame (data,'id'])
df

country time_bucket category    id
0   A     8         staff       101
1   A     8         staff       172
2   A     8         student     122
3   A     8         student     122
4   B     8         student     142
5   B     9         staff       132

我想要这样的东西:

country time_bucket category    id  staff_count student_count
0   A     8          staff      101     2           1  
1   A     8          staff      172     2           1
2   A     8          student    122     2           1
3   A     8          student    122     2           1
4   B     8          student    142     0           1
5   B     9          staff      132     1           0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)