[英]Alternating values within groups in Pandas
I have the following dataframe:我有以下 dataframe:
df1
a b
4 0 1
5 0 1
6 0 2
2 0 3
3 1 2
15 1 3
12 1 3
13 1 1
15 3 1
14 3 1
8 3 1
9 3 1
10 3 2
I need another column that groups by a,b and assigning the values 1,2 alternating, always starting with 1, within each group of a.我需要另一列按 a,b 分组,并在 a 的每一组中交替分配值 1,2,总是从 1 开始。 It should look like this:
它应该如下所示:
a b c
4 0 1 1
5 0 1 1
6 0 2 2
2 0 3 1
3 1 2 1
15 1 3 2
12 1 3 2
13 1 1 1
15 3 1 1
14 3 1 1
8 3 1 1
9 3 1 1
10 3 2 2
Use transform
+ factorize
使用
transform
+ factorize
df.groupby('a').b.transform(lambda x : (x.factorize()[0]+1)//2+1)
4 1
5 1
6 2
2 1
3 2
15 2
12 2
13 2
15 1
14 1
8 1
9 1
Name: b, dtype: int64
One approach could be to groupby
, take the ngroup
which basically enumerates each group, then take the 2
modulo and use the result to index from either 1
or 2
:一种方法可能是
groupby
,采用基本上枚举每个组的ngroup
,然后采用2
模并使用结果从1
或2
索引:
import numpy
df['c'] = np.array([1,2])[df.groupby(['a','b']).ngroup().values%2]
print(df)
a b c
4 0 1 1
5 0 1 1
6 0 2 2
2 1 2 1
3 1 3 2
15 1 3 2
12 1 3 2
13 1 4 1
15 3 1 2
14 3 1 2
8 3 1 2
9 3 1 2
Update so that each now group in a
starts with 1
(adapting @wen's answer):更新以使 a 中的每个 now 组都以
a
开头1
改编@wen 的答案):
df['c'] = df.groupby('a').b.transform(lambda x : x.factorize()[0]%2+1)
print(df)
a b c
4 0 1 1
5 0 1 1
6 0 2 2
2 0 3 1
3 1 2 1
15 1 3 2
12 1 3 2
13 1 1 1
15 3 1 1
14 3 1 1
8 3 1 1
9 3 1 1
10 3 2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.