In R one can easily add sequence along the two (or even more) condition variables using ave(), like this:
# create a dataframe
dat = data.frame(
FactorA = c(rep('a1', 10), rep('a2', 10)),
FactorB = c(rep('b1', 5), rep('b2', 5), rep('b1', 5), rep('b2', 5)),
DependentVar = rnorm(20)
)
# add ordering given combination of two factors
dat$Order <- ave(dat$DependentVar, dat$FactorA, dat$FactorB,
FUN=seq_along)
What would be an analogue in Python with pandas?
Addition on 22/06/2020:
Also, if you would make the levels of FactorA and FactorB interleave by "shuffling" them, like this, for example:
# a slightly "shuffled" dataframe
dat2 = data.frame(
FactorA = c(rep('a1', 6), rep('a2', 6),
rep('a1', 4), rep('a2', 4)),
FactorB = c(rep('b1', 3), rep('b2', 3), rep('b1', 3), rep('b2', 3),
rep('b1', 2), rep('b2', 2), rep('b1', 2), rep('b2', 2)),
DependentVar = rnorm(20)
)
ave() would continue to sequence them along:
dat2$Order <- ave(dat2$DependentVar, dat2$FactorA, dat2$FactorB,
FUN=seq_along)
dat2
FactorA FactorB DependentVar Order
1 a1 b1 1.3814360 1
2 a1 b1 1.0702582 2
3 a1 b1 -1.1974390 3
4 a1 b2 -1.1687711 1
5 a1 b2 -0.7584645 2
6 a1 b2 -0.5541912 3
7 a2 b1 -0.3083331 1
8 a2 b1 0.7707984 2
9 a2 b1 2.4709730 3
10 a2 b2 0.1768273 1
11 a2 b2 0.5687605 2
12 a2 b2 0.7360105 3
13 a1 b1 0.9253223 4
14 a1 b1 -0.3190011 5
15 a1 b2 -0.2657454 4
16 a1 b2 -0.1617810 5
17 a2 b1 0.9634501 4
18 a2 b1 -0.6749173 5
19 a2 b2 0.8138765 4
20 a2 b2 -1.1075720 5
Can Python (1) mark the "appearance" of the combination and, also, (2) reset the sequencing, like this:
FactorA FactorB DependentVar Order OrderReset WhichAppearance
1 a1 b1 1.3814360 1 1 1
2 a1 b1 1.0702582 2 2 1
3 a1 b1 -1.1974390 3 3 1
4 a1 b2 -1.1687711 1 1 1
5 a1 b2 -0.7584645 2 2 1
6 a1 b2 -0.5541912 3 3 1
7 a2 b1 -0.3083331 1 1 1
8 a2 b1 0.7707984 2 2 1
9 a2 b1 2.4709730 3 3 1
10 a2 b2 0.1768273 1 1 1
11 a2 b2 0.5687605 2 2 1
12 a2 b2 0.7360105 3 3 1
13 a1 b1 0.9253223 4 1 2
14 a1 b1 -0.3190011 5 2 2
15 a1 b2 -0.2657454 4 1 2
16 a1 b2 -0.1617810 5 2 2
17 a2 b1 0.9634501 4 1 2
18 a2 b1 -0.6749173 5 2 2
19 a2 b2 0.8138765 4 1 2
20 a2 b2 -1.1075720 5 2 2