Python Dataframe Result Based On Values Of 2 Different Columns
Say I have the following dataframe. import pandas as pd df = pd.DataFrame() df['close'] = (7980,7996,8855,8363,8283,8303,8266,8582,8586,8179,8206,7854,8145,8152,8240,8373,8319,829
Solution 1:
you can use np.select
after changing the '-' in outcome1 with the following available value by replace
the -
by nan
and bfill
.
s = df['outcome1'].replace('-', np.nan).bfill().astype(float)
conds = [df['A'].eq('TDOWN')&df['close'].gt(s),
df['A'].eq('BUP')&df['close'].lt(s)]
choices = ['SELL','BUY']
df['B'] = np.select(conds, choices, '-')
print (df.head(20))
close A outcome1 B
0 7980 TDOWN - -
1 7996 TDOWN - -
2 8855 TDOWN - SELL
3 8363 TOP - -
4 8283 TOP - -
5 8303 TOP - -
6 8266 TOP - -
7 8582 TOP 8582 -
8 8586 BUP - -
9 8179 BUP - -
10 8206 BUP - -
11 7854 BUP - BUY
12 8145 BUP - BUY
13 8152 BOTTOM 8152 -
14 8240 BOTTOM - -
15 8373 BOTTOM - -
16 8319 BUP - -
17 8298 BUP - -
18 8048 BUP - BUY
19 8218 BUP - -
Post a Comment for "Python Dataframe Result Based On Values Of 2 Different Columns"