简体   繁体   English

Python pandas循环值以两列为条件

[英]Python pandas loop value conditional on two columns

In my dataframe 'data' I have two columns 'trend' & 'rtrend' 在我的数据框“数据”中,我有两列'trend'和'rtrend'

trend has values -1, 0 and 1. trend值为-1,0和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 . 如果trend等于-1或1,那么我想将rtrend列值设置为trend

If trend equals 0, then set rtrend equal to the last value in that series which appears above in the dataframe. 如果trend等于0,则将rtrend设置rtrend等于数据rtrend上方显示的该系列中的最后一个值。

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

All it currently returns is 0 for the whole series. 对于整个系列,它当前返回的全部为0。

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). (我已经尝试过np.where() ,它似乎没有做我想要的事情)。

Don't do a procedural slow for loop. 不要做一个程序缓慢for循环。 Do the vectorized approach. 做矢量化方法。 Just copy non zero data into your new rtrend column, then forward fill the data: 只需将非零数据复制到新的rtrend列中,然后转发填充数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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