Reputation: 3577
I have a dataframe like this:
Day Diff
137 0
185 48
249 64
139 -110
In the column Diff
whenever a negative value is encountered I want to subtract 365 from the value in Day
from the previous row and then add that value to the Day
value in the current row of the negative number. For example, in this scenario when -110
is encountered I want to do 365-249 (249 is from Day
in previous row) and then add 139. So 365-249 = 116 and 116 + 139 = 255. Therefore -110
would be replaced with 255
.
My desired output then is:
Day Diff
137 0
185 48
249 64
139 255
Upvotes: 0
Views: 29
Reputation: 210872
you can do it this way:
In [32]: df.loc[df.Diff < 0, 'Diff'] = 365 + df.Day - df.shift().loc[df.Diff < 0, 'Day']
In [33]: df
Out[33]:
Day Diff
0 137 0.0
1 185 48.0
2 249 64.0
3 139 255.0
Upvotes: 1