Luck Box
Luck Box

Reputation: 90

Fill new Pandas column based on if is not value regex

I am trying to build a new column in a data frame where only the rows that match an if is not are filled. I am currently filling all the rows, which is the issue. In the results below, I shouldn't see the .*_TO_CLOSE rows, which should be None, filling with 'Open'.

def strategy_type():
    for row in TRADER_READER['Action']:
        open = re.search('.*_TO_OPEN$', str(row))
        if open is not None:
            TRADER_READER['Strategy'] = 'Open'
    print(TRADER_READER)


strategy_type()

Returns:

2020-03-27 16:15:00  Receive Deliver  EXPIRE_TO_CLOSE  ...         PUT     Open
2020-03-31 17:00:00  Receive Deliver      BUY_TO_OPEN  ...         NaN     Open
2020-03-31 17:00:00  Receive Deliver  EXPIRE_TO_CLOSE  ...         PUT     Open
2020-04-01 11:00:05            Trade    SELL_TO_CLOSE  ...         NaN     Open
2020-04-01 11:00:05            Trade    SELL_TO_CLOSE  ...         PUT     Open

Upvotes: 0

Views: 361

Answers (3)

umbreon29
umbreon29

Reputation: 233

What you're code is doing is every time the regex not returns none you're creating the column strategy again. I would try something like

def check_open(x):
  open = re.search('.*_TO_OPEN$', str(x))
  return 'Open' if open is not None else None

TRADER_READER['strategy'] = TRADER_READER['action'].apply(check_open)

Upvotes: 1

wwnde
wwnde

Reputation: 26676

If looking for a vectorised solution.Look at the following. Consider change in column names maybe different with yours

           Date  time        deliver            status              
0   2020-03-27  16:15:00    Receive-Deliver     EXPIRE_TO_CLOSE     
1   2020-03-31  17:00:00    Receive-Deliver     BUY_TO_OPEN 
2   2020-03-31  17:00:00    Receive-Deliver     EXPIRE_TO_CLOSE 
3   2020-04-01  11:00:05    Trade               SELL_TO_CLOSE   
4   2020-04-01  11:00:05    Trade               SELL_TO_CLOSE   

apply mask

m=df.status.str.contains(r'TO_OPEN')
m

Fill column

df.loc[m,'availability']='open'
df

Outcome

enter image description here

Upvotes: 2

Renaud
Renaud

Reputation: 2819

I think it could be easier and quicker to use str.contains function of pandas: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html

TRADER_READER['Action'].astype(str) 
StratDictionary = {True: 'Open', False: 'Close' } 
TRADER_READER['Strategy']=TRADER_READER['Action'].str.contains('.*_TO_OPEN$', regex=True).replace(StratDictionary)

Upvotes: 1

Related Questions