python - Subtraction/Addition from seperate rows/columns -
i have dataframe this:
day diff 137 0 185 48 249 64 139 -110
in column diff
whenever negative value encountered want subtract 365 value in day
previous row , add value day
value in current row of negative number. example, in scenario when -110
encountered want 365-249 (249 day
in previous row) , add 139. 365-249 = 116 , 116 + 139 = 255. therefore -110
replaced 255
.
my desired output is:
day diff 137 0 185 48 249 64 139 255
you can 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
Comments
Post a Comment