3

I have table in df:

X1  X2
1   1
1   2
2   2
2   2
3   3
3   3

And i want calculate Y, where Y = Yprevious + 1 if X1=X1previous and X2=X2previous, elso 0. Y on first line = 0. Example.

X1  X2  Y
1   1   0
2   2   0
2   2   1
2   2   2
2   2   3
3   3   0

Not a duplicate... Previously, the question was simpler - addition with a value in a specific line. Now the term appears in the calculation process. I need some cumulative calculation

What I need, more example:

X1  X2  Y
1   1   0
2   2   0
2   2   1
2   2   2
2   2   3
3   3   0
3   3   1
2   2   0

What I get on the link to the duplicate

X1  X2  Y
1   1   0
2   2   0
2   2   1
2   2   2
2   2   3
3   3   0
3   3   1
2   2   4
asymon
  • 187
  • 9

1 Answers1

1

Use GroupBy.cumcount with new columns by consecutive values:

df1 = df[['X1','X2']].ne(df[['X1','X2']].shift()).cumsum()

df['Y'] = df.groupby([df1['X1'], df1['X2']]).cumcount()
print (df)
   X1  X2  Y
0   1   1  0
1   2   2  0
2   2   2  1
3   2   2  2
4   2   2  3
5   3   3  0
6   3   3  1
7   2   2  0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252