Reputation: 1629
I have a stock dataframe:
Open High Low Close Volume rb us \
0 1.20821 1.20821 1.20793 1.20794 138.96 0.022347 100.000000
1 1.20794 1.20795 1.20787 1.20788 119.61 0.004967 85.714286
2 1.20788 1.20793 1.20770 1.20779 210.42 0.007451 64.285714
3 1.20779 1.20791 1.20779 1.20789 77.51 0.008280 83.333333
4 1.20789 1.20795 1.20789 1.20792 56.97 0.002484 50.000000
ls color
0 96.428571 black
1 85.714286 black
2 50.000000 black
3 100.000000 white
4 100.000000 white
I want to compare current row data to previous row data,something like this:
if(df['color'] == df['color'].shift(-1)):
if((df['Close'] >= df['Open'].shift(-1) and df['Open']>=df['Close'].shift(-1)):
df['Position'] = UP
if((df['Close'] < df['Open'].shift(-1) and df['Open']<=df['Close'].shift(-1)):
df['Position'] = DOWN
THERE ARE MANY MORE IF CONDITIONS ...
It's not possible to compare data by
np.where(condition,TRUE,FALSE)
Because of many conditions I have in my algorithm. It's just part of them.
How can I do these conditions check?
Upvotes: 2
Views: 12209
Reputation: 18916
I would divide it in different check-ups:
diffdf = df.diff()[1:]
cond1 = diffdf['Volume'] > 0
cond2 = diffdf['Close'] >= 0
# cond3 = diffdf['Open'] >= 0 ?? What is this supposed to check?
df['Position'] = np.insert(np.where(cond1&cond2, 'UP', 'DOWN'), 0, '-')
df.diff()
:
Open High Low Close Volume
0 NaN NaN NaN NaN NaN
1 -0.00027 -0.00026 -0.00006 -0.00006 -19.35
2 -0.00006 -0.00002 -0.00017 -0.00009 90.81
3 -0.00009 -0.00002 0.00009 0.00010 -132.91
4 0.00010 0.00004 0.00010 0.00003 -20.54
Full example:
import pandas as pd
import numpy as np
data = '''\
Open High Low Close Volume
1.20821 1.20821 1.20793 1.20794 138.96
1.20794 1.20795 1.20787 1.20788 119.61
1.20788 1.20793 1.20770 1.20779 210.42
1.20779 1.20791 1.20779 1.20789 77.51
1.20789 1.20795 1.20789 1.20792 56.97'''
fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, sep='\s+')
# Our client wants to know what stocks have increased in Volumne and Close
# We call this element Position and it is either Up,Down,-
# Let us create a difference dataframe and check those conditions
diffdf = df.diff()[1:]
cond1 = diffdf['Volume'] > 0
cond2 = diffdf['Close'] >= 0
df['Position'] = np.insert(np.where(cond1&cond2, 'UP', 'DOWN'), 0, '-')
print(df)
Returns:
Open High Low Close Volume Position
0 1.20821 1.20821 1.20793 1.20794 138.96 -
1 1.20794 1.20795 1.20787 1.20788 119.61 DOWN
2 1.20788 1.20793 1.20770 1.20779 210.42 DOWN
3 1.20779 1.20791 1.20779 1.20789 77.51 DOWN
4 1.20789 1.20795 1.20789 1.20792 56.97 DOWN
Upvotes: 2
Reputation: 8631
You need:
df['Position'] = np.where((df['Volume'] > df['Volume'].shift(-1)) & ((df['Close'] >= df['Close'].shift(-1)) & (df['Open'] <= df['Open'])),"UP","DOWN")
Output:
Open High Low Close Volume Position
0 1.20821 1.20821 1.20793 1.20794 138.96 UP
1 1.20794 1.20795 1.20787 1.20788 119.61 DOWN
2 1.20788 1.20793 1.20770 1.20779 210.42 DOWN
3 1.20779 1.20791 1.20779 1.20789 77.51 DOWN
4 1.20789 1.20795 1.20789 1.20792 56.97 DOWN
Upvotes: 8