Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas loop value conditional on two columns

In my dataframe 'data' I have two columns 'trend' & 'rtrend'

trend has values -1, 0 and 1.

def newfunc(a):

j = -1

for i in a:

    j = j+1
    x = (j-1)

    if data.iloc[j]['trend'] != 0:

        return data.iloc[j]['trend'] 

    if data.iloc[j]['trend'] == 0:

        return data.iloc[x]['rtrend']

If trend is equal to -1 or 1 then I'd like to set the rtrend column value equal to trend.

If trend equals 0, then set rtrend equal to the last value in that series which appears above in the dataframe.

data['rtrend'] = newfunc(data['trend'])

All it currently returns is 0 for the whole series.

Please could someone point me in the right direction? I'm sure there must be a better way to do this. (I've tried np.where() which doesn't seem to do what I'm after).

like image 493
Matthewj28 Avatar asked Sep 10 '26 06:09

Matthewj28


1 Answers

Don't do a procedural slow for loop. Do the vectorized approach. Just copy non zero data into your new rtrend column, then forward fill the data:

df['rtrend'] = df[df.trend!=0]['trend']

df
Out[21]: 
   trend    b    c  rtrend
a   -1.0  1.0 -1.0    -1.0
c    0.0 -1.0  1.0     NaN
e    1.0 -1.0 -1.0     1.0
f   -1.0  1.0 -1.0    -1.0
h   -1.0  1.0  1.0    -1.0

df['rtrend'].ffill()
Out[22]: 
a   -1.0
c   -1.0
e    1.0
f   -1.0
h   -1.0
Name: rtrend, dtype: float64
like image 77
Zeugma Avatar answered Sep 12 '26 19:09

Zeugma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!