Reputation: 262
I'm trying to iterate thru a dataframe. My goal would be to increase the value in the column loop
when in the column rpm
the previous row and the actual row aren't equal.
df = pd.DataFrame({'rpm': [5000, 5000, 10000, 10000, 15000, 15000],
'temp': [23, 23, 24, 23, 24, 25]})
df['loop'] = 0
def loop_no(x,y):
if x.rpm != y.rpm:
val = y.loop + 1
else:
val = x.loop
return val
df['loop'] = df.apply(lambda x: loop_no(x, x.shift(-1)))
At this point, I get this error:
AttributeError: ("'Series' object has no attribute 'rpm'", u'occurred at index rpm').
When I use axis=1
, I don't get an error. But obviously it shifts then in column direction. So, I don't get the previous line.
df['loop'] = df.apply(lambda x: loop_no(x, x.shift(-1)), axis=1)
Upvotes: 0
Views: 354
Reputation: 323226
IIUC
(df.rpm!=df.rpm.shift(-1)).cumsum()
Out[796]:
0 0
1 1
2 1
3 2
4 2
5 3
Name: rpm, dtype: int32
More info
df['loop']=(df.rpm!=df.rpm.shift(-1)).cumsum()
df
Out[799]:
rpm temp loop
0 5000 23 0
1 5000 23 1
2 10000 24 1
3 10000 23 2
4 15000 24 2
5 15000 25 3
Upvotes: 1