python 2.7 - How do I update the values in a pandas dataframe column until first occurance of a value in the same column? -


i have following dataframe -

              50d-200d  regime   date                           2017-02-22       nan       0   2017-02-23       nan       0   2017-02-24       nan       0   2017-02-27      0.52       1   2017-02-28      0.92       1   ... 2017-04-04      0.39       1   2017-04-05      0.16       1   2017-04-06     -0.08      -1   2017-04-07     -0.30      -1   2017-04-10     -0.51      -1 ... 2017-08-09     -1.15      -1   2017-08-10     -0.52      -1   2017-08-11      0.07       1   2017-08-17      2.67       1 

i want modify dataframe such "regime" column values set 0 until first occurance of "-1". after that, leave dataframe unmodified. how achieve this?

tia

use idxmax index value of first -1 , set 0:

idx = df['regime'].eq(-1).idxmax() df.iloc[:df.index.get_loc(idx), df.columns.get_loc('regime')] = 0 print (df)             50d-200d  regime date                         2017-02-22       nan       0 2017-02-23       nan       0 2017-02-24       nan       0 2017-02-27      0.52       0 2017-02-28      0.92       0 2017-04-04      0.39       0 2017-04-05      0.16       0 2017-04-06     -0.08      -1 2017-04-07     -0.30      -1 2017-04-10     -0.51      -1 2017-08-09     -1.15      -1 2017-08-10     -0.52      -1 2017-08-11      0.07       1 2017-08-17      2.67       1 

and solution pirsquared, you:

df.iloc[:df.regime.eq(-1).values.argmax(), df.columns.get_loc('regime')] = 0 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -