I have sorted data like this:
Id
A
A
B
C
D
D
D
D
E
and I need to check if value one row above is the same. If it isn't, in new column ['value'] should get value 1 but if it is new column should be ['value'] + 1.
I started from doing new column ['Previous_id'] and using .shift()
df['Previous_id'] = df['Id'].shift(1)
So I get frame like this:
Id Previous_id
A Nan
A A
B A
C B
D C
D D
D D
D D
E D
But if I'm trying use .shift in function
def func1(row):
if row['Id'] != row['Previous_id']:
return 1
else:
return row['value'].shift(1) + 1
df['value'] = df.apply(lambda row: func1(row), axis=1)
I get an error:
'int' object has no attribute 'shift'
I'm looking for solution like this:
Example:
Id Previous_id value
A Nan 1
A A 2
B A 1
C B 1
D C 1
D D 2
D D 3
D D 4
E D 1