Create a column which increments value for changes in another row

后端 未结 3 1258
逝去的感伤
逝去的感伤 2021-02-09 09:55

I have a dataframe with two columns as below:

Var1Var2
a   28
b   28
d   28
f   29
f   29
e   30
b   30
m   30
l   30
u   31
t   31
t   31

I\'d

3条回答
  •  隐瞒了意图╮
    2021-02-09 10:23

    You can compare Var2 with its shifted-by-1 version:

    v
       Var1  Var2
    a     0    28
    b     1    28
    d     2    28
    f     3    30
    f     4    30
    e     5     2
    b     6     2
    m     7     2
    l     8     2
    u     9     5
    t    10     5
    t    11     5
    
    i = v.Var2    
    v['Var3'] = i.ne(i.shift()).cumsum()
    
    v
       Var1  Var2  Var3
    a     0    28     1
    b     1    28     1
    d     2    28     1
    f     3    30     2
    f     4    30     2
    e     5     2     3
    b     6     2     3
    m     7     2     3
    l     8     2     3
    u     9     5     4
    t    10     5     4
    t    11     5     4
    

提交回复
热议问题