4

I want to select the previous row's value only if it meets a certain condition E.g.

df:
Value  Marker  
10  0  
12  0  
50  1  
42  1  
52  0  
23  1

I want to select the previous row's value where marker == 0if the current value marker == 1.

Result:

df:
Value  Marker  Prev_Value  
10  0  nan
12  0  nan
50  1  12
42  1  12
52  0  nan
23  1  52

I tried: df[prev_value] = np.where(df[marker] == 1, df[Value].shift(), np.nan) but that does not take conditional previous value like i want.

2 Answers 2

3
condition = (df.Marker.shift() == 0) & (df.Marker == 1)
df['Prev_Value'] = np.where(condition, df.Value.shift(), np.nan)

Output:

df
   Value  Marker  Prev_Value
0     10       0         NaN
1     12       0         NaN
2     50       1        12.0
3     42       1         NaN
4     52       0         NaN
5     23       1        52.0
Sign up to request clarification or add additional context in comments.

Comments

0

You could try this:

df['Prev_Value']=np.where(dataframe['Marker'].diff()==1,dataframe['Value'].shift(1, axis = 0),np.nan)

Output:

df

   Value  Marker  Prev_Value
0     10       0   NaN
1     12       0   NaN
2     50       1  12.0
3     42       1   NaN
4     52       0   NaN
5     23       1  52.0

If you want to get the previous non-1 marker value, if marker==1, you could try this:

prevro=[]
for i in reversed(df.index):

    if df.iloc[i,1]==1:
        prevro_zero=df.iloc[0:i,0][df.iloc[0:i,1].eq(0)].tolist()
        if len(prevro_zero)>0:
            prevro.append(prevro_zero[len(prevro_zero)-1])
        else:
            prevro.append(np.nan)
    else:
        prevro.append(np.nan)
        
df['Prev_Value']=list(reversed(prevro))

print(df)

Output:

   Value  Marker  Prev_Value
0     10       0         NaN
1     12       0         NaN
2     50       1        12.0
3     42       1        12.0
4     52       0         NaN
5     23       1        52.0

6 Comments

nope thats not what I want. I want the previous value as shown in result df.
so in case of index[3], I would want the next previous value with marker == 0, which is 12 (index[1]), instead of nan.
I don't get it. The condition isn't that previous row's value where marker == 0 if the current value marker == 1?. At index[3] you current marker value is 1 and previous row's marker value is 1, not 0.
so the thing is when I have 1 in my current row, i want to lookup the previous non-1 value and return that as previous value. Is it clearer?
Yeah, that's cleaner. Just edited my answer to that condition, checkout it. And as a suggestion, try to be more clear with the conditions, because previous row's value where marker == 0 if the current value marker == 1 means to check the the immediate previous row, which is different to check the previous non-1 rows.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.