每组的唯一值计数作为带有 pandas 的新列 [英] Count of unique values per group as new column with pandas

查看:60
本文介绍了每组的唯一值计数作为带有 pandas 的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想统计一组在熊猫数据框中的唯一观察值,并创建一个具有唯一计数的新列.重要的是,我不想减少数据框中的行.有效地执行类似于SQL中的窗口函数的操作.

I would like to count the unique observations by a group in a pandas dataframe and create a new column that has the unique count. Importantly, I would not like to reduce the rows in the dataframe; effectively performing something similar to a window function in SQL.

df = pd.DataFrame({
         'uID': ['James', 'Henry', 'Abe', 'James', 'Henry', 'Brian', 'Claude', 'James'],
         'mID': ['A', 'B', 'A', 'B', 'A', 'A', 'A', 'C']
})

df.groupby('mID')['uID'].nunique()

将获得每组的唯一计数,但它会汇总(减少行数),我实际上想按照以下方式做点事情:

Will get the unique count per group, but it summarises (reduces the rows), I would effectively like to do something along the lines of:

df['ncount'] = df.groupby('mID')['uID'].transform('nunique')

(这显然不起作用)

通过采用唯一的汇总数据框并将其连接到原始数据框,可以实现所需的结果,但是我想知道是否有更简单的解决方案.

It is possible to accomplish the desired outcome by taking the unique summarised dataframe and joining it to the original dataframe but I am wondering if there is a more minimal solution.

谢谢

推荐答案

GroupBy.transform('nunique')

v0.23.4 上,您的解决方案对我有用.

GroupBy.transform('nunique')

On v0.23.4, your solution works for me.

df['ncount'] = df.groupby('mID')['uID'].transform('nunique')
df
      uID mID  ncount
0   James   A       5
1   Henry   B       2
2     Abe   A       5
3   James   B       2
4   Henry   A       5
5   Brian   A       5
6  Claude   A       5
7   James   C       1


GroupBy.nunique + pd.Series.map

此外,使用您现有的解决方案,您可以将系列映射回到 mID :

df['ncount'] = df.mID.map(df.groupby('mID')['uID'].nunique())
df
      uID mID  ncount
0   James   A       5
1   Henry   B       2
2     Abe   A       5
3   James   B       2
4   Henry   A       5
5   Brian   A       5
6  Claude   A       5
7   James   C       1

这篇关于每组的唯一值计数作为带有 pandas 的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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