0

I have DataFrame having column with Chat Transcript.

ID  Chat
1   P1: Please call me soon P2 - will call you
2   P2: Please call me soon P1 - will call you

I want to create a new column with yes/No or 1/0 if chat has pattern "call me soon" only between P1 & P2 But not between P2 & P1.

Out put will be like

ID  Chat                                          Call me soon
1   P1: Please call me soon P2 - will call you        Yes
2   P2: Please call me soon P1 - will call you         No

I need to do it in python. Please suggest a appropriate method.

1 Answer 1

2

Use str.contains + np.where:

df['Call me soon'] = np.where(
    df.Chat.str.contains('(?<=P1).*?call me soon.*?(?=P2)'), 'Yes', 'No'
)

df
   ID                                        Chat Call me soon
0   1  P1: Please call me soon P2 - will call you          Yes
1   2  P2: Please call me soon P1 - will call you           No

Regex Details

(?<=P1)       # lookbehind, match P1
.*?           # any character - non-greedy
call me soon
.*?       
(?=P2)        # lookahead, match P2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.