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

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -