简体   繁体   中英

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM