'How using function np.where along with apply lambda
this code:
def nearest_independment(target):
lst=df[df['CLINE_TYPE'].str.contains('crease') &
df['CLINE_TYPE'].isin(['nan']).shift(2) &
df['CLINE_TYPE'].isin(['nan']).shift(3)].index
return min(lst, key=lambda x: abs(x - target))
df['FIGURE_TYPE'] = df.apply(lambda x:
np.where(bool(re.match(r'crease',df.loc[x.name,'CLINE_TYPE']))==True &
df.loc[x.name-2,'CLINE_TYPE']=='nan' &
df.loc[x.name-3,'CLINE_TYPE']=='nan',
'INDEPENDENT',
np.where((bool(re.match(r'crease',df.loc[x.name-2,'CLINE_TYPE']))==True |
bool(re.match(r'crease',df.loc[x.name-3,'CLINE_TYPE']))==True) &
bool(re.match(r'Increase',df.loc[x.name,'CLINE_TYPE']))==True &
((df.loc[x.name,'Close_Max_FIG'] > df.loc[nearest_independment(x.name),'Open_Max_COR']) |
(df.loc[x.name,'Close_Max_FIG'] > df.loc[nearest_independment(x.name),'Close_Max_COR']))&
((bool(re.match(r'crease',df.loc[x.name+2,'CLINE_TYPE']))==True | df.loc[x.name+2,'CLINE_TYPE']=='nan') |
(bool(re.match(r'crease',df.loc[x.name+3,'CLINE_TYPE']))==True | df.loc[x.name+3,'CLINE_TYPE']=='nan')),
'TRANZIT_UP',np.nan)),axis=1
)
print(df['FIGURE_TYPE'])
Problem in using df.loc, return eror 'KeyError: -2' the reason is here 'df.loc[x.name-2',logically, such a key does not exist. If you refuse to use 'loc', then the result of df['FIGURE_TYPE'] is a list of a series of values: 'nan','INDEPENDENT','TRANZIT_UP'. But, I need one value of the checked row 'lambda'. Therefore, I use 'loc'. Next, I use pd.Series for the desired result.
Data:
CLINE_TYPE Close_Max_FIG Open_Max_COR Close_Max_COR
0 nan NaN NaN NaN
1 nan NaN NaN NaN
2 nan NaN NaN NaN
3 Increase_SUPERPOSITION 29837.29 29840.65 30045.85
4 nan NaN NaN NaN
5 Decrease_FALSE 29837.29 NaN NaN
6 nan NaN NaN NaN
7 Increase 30045.85 NaN NaN
8 nan NaN NaN NaN
9 nan NaN NaN NaN
10 nan NaN NaN NaN
11 nan NaN NaN NaN
Need to compare the value of rows 7 with the values of row 3. Line 3 is obtained from 'nearest_independment'.
example not 'loc':
df['FIGURE_TYPE'] = df.apply(lambda x:
np.where(df['CLINE_TYPE'].str.contains('crease') &
df['CLINE_TYPE'].isin(['nan']).shift(2) &
df['CLINE_TYPE'].isin(['nan']).shift(3) ,
'INDEPENDENT',
np.where((df['CLINE_TYPE'].str.contains('crease').shift(2) |
df['CLINE_TYPE'].str.contains('crease').shift(3)) &
df['CLINE_TYPE'].str.contains('Increase') &
((df.loc[x.name,'Close_Max_FIG'] > df.loc[nearest_independment(x.name),'Open_Max_COR']) |
(df.loc[x.name,'Close_Max_FIG'] > df.loc[nearest_independment(x.name),'Close_Max_COR']))&
((df['CLINE_TYPE'].str.contains('crease').shift(-2) | df['CLINE_TYPE'].isin(['nan']).shift(-2)) |
(df['CLINE_TYPE'].str.contains('crease').shift(-3) | df['CLINE_TYPE'].isin(['nan']).shift(-3))),
'TRANZIT_UP',np.nan)),axis=1
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|