Reputation: 45
Imagine that you have a simple dataframe as such
low_signal high_signal condition prevision
0 1.2 1.3 1 1.7
1 1.7 1.8 1 1.8
2 1.9 2.0 1 1.9
3 1.6 1.7 -1 1.5
4 1.3 1.4 -1 1.4
Now I want to study if my prediction is correct and if so I would like to know the time (which in this case we can say that is equal to the index)
My first condition
if df.condition == 1
the second is to check if df.prevision
will ever be lower or equal than the df.low_signal
.
If that condition is True then the
return is the index (in study) of the df.predicted - the index of df.low_signal
low_signal high_signal condition prevision verification
0 1.2 1.3 1 1.7 1
1 1.7 1.8 1 1.8 1
2 1.9 2.0 1 1.9 0
3 1.6 1.7 -1 1.5 1
4 1.3 1.4 -1 1.4 0
That is just for df.condition == 1
For df.condition == -1
is almost the same, however the check is if the df.prevision will ever be greater or equal than the df.high_signal
Upvotes: 2
Views: 92
Reputation: 3947
A simple way to solve it:
In [18]:
df['verification'] = ((df['high_signal'] <= df['prevision']) & (df['condition'] == 1)) \
| ((df['low_signal'] <= df['prevision']) & (df['condition'] == -1))
In [19]:
df
Out[19]:
low_signal high_signal condition prevision verification
0 1.2 1.3 1 1.7 True
1 1.7 1.8 1 1.8 True
2 1.9 2.0 1 1.9 False
3 1.6 1.7 -1 1.5 False
4 1.3 1.4 -1 1.4 True
Probably you know that in python True and False correspond to 1 and 0, but if you want to see the verification
column as int, just do:
In [20]:
df['verification'] = df['verification'].astype(int)
Hope it helps.
EDIT: I update the answer as per your comments. I don't really understand the 'functional requirement', but anyway if you have a more complex operation you can use pandas' apply
In your case is a bit tricky since:
apply
function, and I don't think you canBut anyway, solving the expression you've put in your comment, you can first add a column with a copy of the index in your dataframe,
In [27]:
df['index_cp'] = df.index
and then use the apply function,
In [28]:
len_df = len(df)
def f(x):
for n in range(len_df):
if x.condition == 1 & (x.prevision <= df.low_signal[n]):
return n - x.index_cp
df.apply(f, axis=1)
Out[28]:
0 1
1 1
2 0
3 NaN
4 NaN
dtype: float64
Probably you need to modify the function but as I say I did not get the "functional" view and I've just implemented what you've posted. Hope it helps.
Upvotes: 2