[英]How to count accumulative unique values by groups in python?
I wonder how to count accumulative unique values by groups in python?我想知道如何按组计算 python 中的累积唯一值?
Below is the dataframe example:下面是 dataframe 示例:
Group![]() |
Year![]() |
Type![]() |
---|---|---|
A![]() |
1998 ![]() |
red![]() |
A![]() |
2002 ![]() |
red![]() |
A![]() |
2005 ![]() |
blue![]() |
A![]() |
2008 ![]() |
blue![]() |
A![]() |
2009 ![]() |
yello![]() |
B![]() |
1998 ![]() |
red![]() |
B![]() |
2001 ![]() |
red![]() |
B![]() |
2003 ![]() |
red![]() |
C ![]() |
1996 ![]() |
red![]() |
C ![]() |
2002 ![]() |
orange![]() |
C ![]() |
2008 ![]() |
blue![]() |
C ![]() |
2012 ![]() |
yello![]() |
I need to create a new column by Column "Group".我需要按“组”列创建一个新列。 The value of this new column should be the accumulative unique values of Column "Type", accumulating by Column "Year".
这个新列的值应该是列“类型”的累积唯一值,按列“年”累积。
Below is the dataframe I want.下面是我想要的dataframe。 For example: For group A and in Year 1998, the accumulative unique values of "Type" is 1. For group A and in Year 2005, the accumulative unique values of "Type" is 2. For group C and in Year 2012, the accumulative unique values of "Type" is 4.
例如:对于 A 组,在 1998 年,“类型”的累积唯一值为 1。对于 A 组,在 2005 年,“类型”的累积唯一值为 2。对于 C 和 2012 年, “类型”的累积唯一值是 4。
| Group| Year| Type|Want|
|------|-----|-----|----|
|A|1998|red|1|
|A|2002|red|1|
|A|2005|blue|2|
|A|2008|blue|2|
|A|2009|yello|3|
|B|1998|red|1|
|B|2001|red|1|
|B|2003|red|1|
|C|1996|red|1|
|C|2002|orange|2|
|C|2008|blue|3|
|C|2012|yello|4|
One more thing about this dataframe: not all groups have values in the same years.关于此 dataframe 的另一件事:并非所有组在同一年份都有值。 For example, group A has values in year 1998,2002,2005, and 2008. group B has values in year 1998, 2001, and 2003.
例如,A 组在 1998、2002、2005 和 2008 年有值。B 组在 1998、2001 和 2003 年有值。
I wonder how to address this problem.我想知道如何解决这个问题。 Your great help means a lot to me.
您的大力帮助对我来说意义重大。 Thanks!
谢谢!
Use custom lambda function with factorize
in GroupBy.transform
:在
GroupBy.transform
中使用自定义 lambda function 和factorize
:
f = lambda x: pd.factorize(x)[0]
df['Want1'] = df.groupby('Group', sort=False)['Type'].transform(f) + 1
print (df)
Group Year Type Want1
0 A 1998 red 1
1 A 2002 red 1
2 A 2005 blue 2
3 A 2008 blue 2
4 A 2009 yello 3
5 B 1998 red 1
6 B 2001 red 1
7 B 2003 red 1
8 C 1996 red 1
9 C 2002 orange 2
10 C 2008 blue 3
11 C 2012 yello 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.